I’m trying to sort column Foo that has one Bar. Currently, I’m using a join so that in the foos#index action is ordered by bars.name*:
@foos = Foo.joins(:bar).order("bars.name")
The problem is, not all foos have a bar, and it seems as though this join ignores any Foo with no Bar. I don’t want these records ignored. Is there a way to do this?
*For the record, I’m using will_paginate, so the index action actually includes:
@foos = Foo.joins(:bar).paginate :page => params[:page], :order => "bars.name"
You need to use SQL
LEFT JOINin yourjoinsstatement. This will fetch all Foos (with Foos that have Bars)To use ‘Rails way’ you can wrap it into the scope: