Nested joins with Arel

Today I had to convert this old Active Record finder to the new Arel backed Active Record:

@promotional_offers = Offer.find(:all, :joins => {:business => :city}, :conditions => {:businesses => {:cities => {:id => @city.id}}})

I couldn’t quickly find docs for Arel on how to do this, but after a couple stabs at it I discovered that this does the trick

 @promotional_offers = Offer.joins(:business => :city).where(:businesses => {:cities => {:id => @city.id}})

It’s good to know that Arel’s joins method takes a hash so we can do nested joins with relative ease.

posted 1 year ago on January 19th, 2011 at 18:50 /