2010-01-12 34 views
1
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 

找到属于foo的物品的最佳方法是什么?我目前使用的是这样的:如何找到多态关系

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

回答

1

以下是语义上等同的,并且更容易阅读和输入。

f = Foo.find 1 
f.items