Polymorphic association

后端 未结 3 487
心在旅途
心在旅途 2021-02-06 05:57

If you have polymorphic belongs_to associations then references will add both of the columns required:

create_table :products do |t|
  t.references :attachment,          


        
3条回答
  •  长发绾君心
    2021-02-06 06:34

    Basically, polymorphic association by definition adds ability to create associations with many other Rails ActiveRecord Models.

    Both Columns are there for Rails to know which Model the association is referring to, you have the attachment_type column (ie String) (default value is "Photo",table-name: photos in the db), and the attachment_id is sort of like a foreign key to that particular Model/table (ie Photo).

    Usually this provides you flexibility to associate one Model to many others.

提交回复
热议问题