开发者

How to find polymorphic relation

开发者 https://www.devze.com 2022-12-16 07:22 出处:网络
class Item < ActiveRecord::Base belongs_to :rulable, :polymorphic => true end class foo < ActiveRecord::Base
class Item < ActiveRecord::Base
belongs_to :rulable, :polymorphic => true
end
class foo < ActiveRecord::Base
has_many :items, :as => rulable
end
class bar < ActiveRecord::Base
has_many :items, :as => rulable
end

What is the best way to find items belonging to a foo? I'm currently using somethi开发者_如何学Pythonng like this:

f = Foo.find 1
Item.find_by_rulable_id_and_rulable_type(f, 'Foo')


The following is semantically equivalent and much easier to read and type.

f = Foo.find 1
f.items
0

精彩评论

暂无评论...
验证码 换一张
取 消