如何在不使用has_many_polymorphs插件的情况下在rails 3中编写以下代码?
has_many_polymorphs :listings, :from => [:my_properties, :friend_properties, :public_properties, :private_properties, :job_listings, :truck_listings, :open_listings, :sale_listings], :through => :group_listings
答案 0 :(得分:0)
此视频帮助我分配#154 Polymorphic Association
在您的Polymorphic表中的键中,您需要 ID 您想要链接到的记录以及要链接到的表的名称。 我通常这样做:
<强>移植强>
create_table "cards", force: true do |t|
t.string '....', default: ""
t.integer "accountable_id"
t.string "accountable_type", default: ""
end
卡片型号
belongs_to :accountable, :polymorphic => true
其他模特
has_many :cards, :as => :accountable
我希望这有助于