[英]How to ActiveRecord Query off of specific association when there are two associations with the same model class
所以可以說我有一個籃球模型。
class Basketball < ActiveRecord::Base
belongs_to :owner, class: User
belongs_to :last_played_with, class: User
end
現在,讓我說我要按照last_played_with的name列的順序從數據庫中獲取籃球。 或所有者的。 在查詢中,如何指定要排序的商品? 這是否需要原始SQL?
您可以使用如下所示的內容:
Basketball.joins(:owner).order("owners.name")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.