Povedz, že mám toto:
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
Vyžaduje si to, aby som tabuľku definoval presne takto?
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
Alebo môžem definovať trochu inak, napríklad s rôznymi stĺpcami alebo typmi, teda spôsobom, ktorý vidím efektívnejšie? Bude polymorfná asociácia naďalej fungovať?
odpovede:
0 pre odpoveď č. 1Polymorfná asociácia sa týka iba _type
a _id
pár stĺpcov. Všetko ostatné je na vás.
Takže áno, ak chcete, môžete pridať ďalšie metadáta.