Explain what is Polymorphic Association in Ruby on Rails?
Explain what is Polymorphic Association in Ruby on Rails?
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:
<model>_type
and <model>_id
to store the associated model name and id[1][2][3]<model>_type
column stores the class name of the associated object[2][3]<model>_id
column stores the id of the associated object[2][3]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
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào