On 10 August 2010 23:02, Fritz Anderson <[email protected]> wrote: > Chris Mear wrote: >> The has_and_belongs_to_many assumes that the join table doesn't have a >> column called 'id' -- you'll see that the SQL it generates just >> SELECTs an 'id' column, so it's (erroneously) picking up the one from >> the join table in your situation. >> >> If your join table is really a fully-fledged model of its own, then >> you need to bring it into the association explicitly, and use the >> :through option to connect media and tour_locations: > ... > > Thanks! This was exactly what I needed to know. > > Further, it greatly simplified my access to subsets of the has_many > :media relationship: > > has_many :images, > :through => :media_tour_locations, > :source => :medium, > :conditions => "media_type = 'image'", > :order => 'media_tour_locations.medium_sequence' > > ... whereby I can use aTourLocation.images and get the images back in > the sequence specified for that relationship. > > I _am_ relying on the :order option to produce a correct ORDER BY > clause. For Rails 2.3.5 and sqlite3 3.6.12, the generated SQL looks > right, and I get the expected results. > > Will this hold up with different versions and back ends, or am I relying > on an implementation detail?
Yeah, as far as I know, Rails doesn't do too much messing around with what you pass in the :order option. So as long as you fully qualify it by including the table name (as you have), it should be solid. Of course, there's no substitute for writing some tests to make sure! Chris -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

