开发者

Rails Join question with has_many relationship

开发者 https://www.devze.com 2023-02-19 22:27 出处:网络
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 referen

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消