Rails :include Gotcha

1 07 2007

If you are using the :include option of the find function in Active Record, there is a big gotcha: you have to disambiguate all column names used in other parameters to ‘find’ (prefix each with the name of the table that contains it).

For example, the following will give you an error:

Post.find(:all, :include => [:user, :comments], :order => “created_at DESC”)

The error that you get is the following: Column ‘created_at’ in order clause is ambiguous
The fix is really easy:

Post.find(:all, :include => [:user, :comments], :order => “post.created_at DESC”)

Note that I changes created_at to post.created_at.
I would have saved myself some debugging time had I known that so I just wanted to share.


Actions

Information

One response to “Rails :include Gotcha”

13 09 2007
MS (22:35:40) :

Thanks, this just saved me from a lot of headaches.

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>