Reputation: 8176
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 something like this:
f = Foo.find 1
Item.find_by_rulable_id_and_rulable_type(f, 'Foo')
Upvotes: 1
Views: 747
Reputation: 23450
The following is semantically equivalent and much easier to read and type.
f = Foo.find 1
f.items
Upvotes: 1