2010-01-12 4 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