Giant Japanese Robot

Better living through pop culture.
Aug 20
Permalink

has_many :through & Acts_As_List

Following post is for Ruby on Rails geeks.

I don’t normally post my programming adventures here, but maybe I should. This is a simple thing and should have been obvious right away to me, but it wasn’t so I am posting it here for future generations. Thanks to Geek Mama for shaming me into posting this.

I have a “has_many :choices, :through => :question_items” model set up for Questions and their Choices. The QuestionItems model also has a”position” column because it is an Acts_As_List model. All is fine and good until I try and get the choices to return in the correct order by calling @questions.choices in my controller.

The solution was pretty simple, adding an :order option to the has_many method solved it:

has_many :choices, :through => :question_items, :order => "question_items.position"
Comments (View)

blog comments powered by Disqus