ruby-on-rails 嵌套关联排序

xkftehaa  于 4个月前  发布在  Ruby
关注(0)|答案(2)|浏览(43)

我有一个应用程序,它的嵌套关联如下所示:

user has_many timesheets
timesheet has_many invoices

字符串
在显示invoice index视图时,我想在user所属的first_name上对invoice进行排序。我可以在直接父代上执行如下排序:

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


有没有可能更上一层楼?

dzhpxtsq

dzhpxtsq1#

是的,它是:

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

字符串

z3yyvxxp

z3yyvxxp2#

除了可接受的答案,如果关联嵌套的深度超过一层,则order SQL字符串中的列名仅包括深度嵌套表的名称。例如:

Invoice.joins(timesheet: [user: :jobs]).order('jobs.completed_at')

字符串

相关问题