Câu hỏi phỏng vấn Ruby on Rails
Câu hỏi

Explain what is Polymorphic Association in Ruby on Rails?

Câu trả lời

A polymorphic association in Ruby on Rails allows a model to belong to more than one other model on a single association. It is a type of Active Record association that provides a way to associate a model with multiple other models on a single association[1][2][3].

The key aspects of polymorphic associations are:

  • A model can belong to multiple models through a single association[1][2]
  • It uses two columns in the database: <model>_type and <model>_id to store the associated model name and id[1][2][3]
  • The <model>_type column stores the class name of the associated object[2][3]
  • The <model>_id column stores the id of the associated object[2][3]
  • It requires a belongs_to declaration in the model with the polymorphic: true option[1][2]

For example, consider a Review model that can be associated with both an Event and a Restaurant model. Using polymorphic associations, the Review model would have a reviewable_type and reviewable_id column to store the associated model name and id[2][3].

``...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What Are Helpers And How To Use Helpers In ROR?

junior

List out what can Rails Migration do?

senior

How would you choose between Belongs_to And Has_one ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào