Eric D. Nielsen
2006-Jun-11 00:54 UTC
[Rails] Reaching through a belongs_to for acts_as_list scope?
I''m running into a situation where I''d like to provide the
list scope
for an acts_as_list model from one step removed. In minimalistic form
(with several other associations removed from each level).
class MainEntity < AcrtiveRecord::Base
has_many :containers
end
class Container < ActiveRecord::Base
has_many :elements
belongs_to :main_entity
end
class Elements < ActiveRecord::Base
belong_to :containers
acts_as_list :scope=> ??? # it wants to be container.main_entity_id
end
Now I''ve thought of two ways to solve this.
1. add main_entity_id to the Element -- this adds a redundant
link in the DB, and I''d like to avoid that, but if this is the normal
Rails solution, I guess I can live with it...
2. Something like
acts_as_list :scope=>''container_id IN (#
{container.main_entity.containers.map { |c|
c.id}.join(",")})''
appears to work, but also appears a little cumbersome.
Is there a better way?
Eric
