Rails Join question with has_many relationship
i'm working on sorting a table based on headers per the Railscasts episode http://asciicasts.com/episodes/228-sortable-table-columns. My table is slightly different in that one of the headers references an associated field. I have everything working peachy EXCEPT that when I sort based on that associated field, it only returns those elements that have an entry for this (optional) field. Here's the code that orders that case:
@user.items.joins(:location).order('locations.title asc')
basically, a user has X many items, each in an optional location. I just want to be able to sort on that location. I'm not sure how to 开发者_开发百科specify that I want all items, not just those that have a location.
Thanks!
If you use include(:location)
instead you'll still get all the items, even if an associated location doesn't exist.
精彩评论