使用嵌套关联进行排序

问题描述 投票:0回答:2
我有一个具有如下嵌套关联的应用程序:

user has_many timesheets timesheet has_many invoices

显示发票索引视图时,我想在其所属的

first_name

user
 上订购发票。我可以像这样对直系父母发出命令:

Invoice.joins(:timesheet).order('timesheets.user_id')

可以再升一级吗?

ruby-on-rails activerecord
2个回答
5
投票
是的,是:

Invoice.joins(timesheet: :user).order('users.first_name')
    

0
投票
除了接受的答案之外,如果关联嵌套超过一层,则

order

 SQL 字符串中的列名仅包含深度嵌套的表的名称。例如:

Invoice.joins(timesheet: [user: :jobs]).order('jobs.completed_at')
    
© www.soinside.com 2019 - 2024. All rights reserved.