Bill Rowell is a Web Developer in Massachusetts, currently specializing in e-commerce development.

billrowell
  • Home
  • Work
  • About
  • Music
  • Books
  • Archives
  • Contact
 
RSS Feed via FeedBurner

Subscribe via RSS or via Email


Colleagues

  • Andrew Teman
  • Conor McNamara
  • Dan Pickett
  • Joe Riopel
  • Ken Bailey
  • Kyle Bradshaw
  • Peter Caputa
  • Scott Jangro

Of Interest

  • Rick Whittington
  • Steve Krug
  • Coding Horror

Accessing “Child” Associations in Ruby on Rails

April 1, 2008 in Development, Ruby on Rails

Ruby on Rails

My good buddy Dan of SecondRotation.com helped me out with a Rails problem last night. I wanted to access the associations defined on an association of one of my model classes when calling find, in essence, accessing a “child” association. I looked high and low for this, but with no luck so Dan was able to come to my rescue. He said you can do this:


@collection = MyClass.find(:all, :conditions => ["id = ?", params[:id]],
      :include => [:foo => [:bar]], :order => sort)

I guess using :include like this is will tell Rails and ActiveRecord that you want to include the Bar class association in the JOIN you’re doing in SQL so you can enhance your query. Makes sense, but too bad it seems to be hardly documented!



  • Glad I could help sir!

    Dan Pickett on April 12, 2008

Leave A Comment

 (required)
 (required)

© Copyright 2007 Bill Rowell, Powered by WordPress 2.3.1