Deirdre Saoirse Moen

Sounds Like Weird

Nasty Little Bugses

13 April 2006

So, it happened that I was doing a rails project where I needed joins in paginate and I wound up discovering something unexpected: foo.id came through as the id for one of the join tables.

The way around that is to find_by_sql instead, and to avoid a similar problem, something like:

def self.find_my_foos(for_which_bar)
  find_by_sql("select foos.* from foos, bars where foos.bar_id =
   bars.id and bars.name = #{for_which_bar} order by foos.position")
end

This does have a side effect of not pre-loading the joins correctly, so that may not be the best solution for your case, but it does fix the larger issue.


Related Posts