说我有这个:
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class Employee < ApplicationRecord
has_many :pictures, as: :imageable
end
class Product < ApplicationRecord
has_many :pictures, as: :imageable
end
这是否要求我以下列方式准确定义表格?
class CreatePictures < ActiveRecord::Migration[5.0]
def change
create_table :pictures do |t|
t.string :name
t.integer :imageable_id
t.string :imageable_type
t.timestamps
end
add_index :pictures, [:imageable_type, :imageable_id]
end
end
或者我可以用不同的列或类型来定义不同的位,例如,在某种程度上我认为更有效?多态关联是否仍然有效?
答案 0 :(得分:0)
多态关联仅与_type
和_id
列对有关。其他一切都取决于你。
是的,如果您愿意,可以添加其他元数据。