I’m a bit stuck on an Active Record query problem. I’d like to do a left join instead of an inner join on a query, but the results of the joins method are returning only an inner join.
Entry.
joins(:measurements => :measurement_type).
sum(:value, :conditions => ['name = ?','Calories'])
The tricky part is that :measurements has a belongs_to relationship to :measurement_type which is where the name column resides.
Right now this is returning sums only for users who have food_posts. The goal is to return sums for all users. Users who don’t have a food post would have a sum of zero.
I’ve tried Googling and searching other posts on here, but haven’t found an answer. Any suggestions?
Also I’m using SQLite in my development environment and Postgresql for production.
If you want to do a left join instead of an inner join you need to use custom ‘sql fragments’
In your example,
Or something along those lines dependent on your setup.
This tutorial sums it up pretty clearly.