Topic: has_many :through, inconsistency with acts_as_list

Just found this, does it seem odd to anyone else?

Given a rather standard has_many :through, with the join model using an acts_as_list to maintain an ordering on one end of the relationship.

class A < ActiveRecord::Base
  has_many :bs
  has_many :cs, :through :b
end

class B < ActiveRecord::Base
  belongs_to :a
  belongs_to :b
  acts_as_list :scope=>:a_id
end

class C < ActiveRecord::Base
  has_many :bs
  has_many :as, :through=>:b
end


a.cs is not ordered according to the acts_as_list, ie a.cs has a different order than b.cs.  (I know I can add an order clause to the has_many :through, but I didn't think I should have to).

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: has_many :through, inconsistency with acts_as_list

Doesn't seem odd to me. SQL isn't guaranteed to return the results in any order unless you specify one. Or were you expecting Rails to automatically sort by the position/acts_as_list column if one isn't specified? Hmm, might worthy of a patch, but I've always assumed it was necessary to specify :order => 'position'.

Edit: I just reread the post and I think I may have miss-understood the problem. Sorry if I did.

Last edited by ryanb (2006-08-09 23:51:35)

Railscasts - Free Ruby on Rails Screencasts

Re: has_many :through, inconsistency with acts_as_list

Well the acts_as_list declaration in class B does ensure the ordering of its Cs, without an explicit order clause on class b.  Class A uses class B to reach throug to C, joining along the scope as the act_as_list, so I would have through it would have used the same order.

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: has_many :through, inconsistency with acts_as_list

NielsenE wrote:

class B < ActiveRecord::Base
  belongs_to :a
  belongs_to :b
  acts_as_list :scope=>:a_id
end

I'm assuming you meant "belongs_to :c" here?

NielsenE wrote:

a.cs is not ordered according to the acts_as_list, ie a.cs has a different order than b.cs.  (I know I can add an order clause to the has_many :through, but I didn't think I should have to).

I'm confused. If b belongs_to :c then there shouldn't be "b.cs" because it only has one c. Or perhaps you meant B has_many :cs. If that's the case then it still makes sense that the order isn't the same because what if multiple bs have the same c?

Railscasts - Free Ruby on Rails Screencasts