使用嵌套关联进行排序

Joh*_*ohn 2 activerecord ruby-on-rails

我有一个具有如下嵌套关联的应用程序:

user has_many timesheets
timesheet has_many invoices
Run Code Online (Sandbox Code Playgroud)

first_name当显示发票索引视图时,我想对它user所属的发票进行排序。我可以像这样对直接父级执行命令:

Invoice.joins(:timesheet).order('timesheets.user_id')
Run Code Online (Sandbox Code Playgroud)

是否可以再升一级?

Mar*_*pka 5

是的:

Invoice.joins(timesheet: :user).order('users.first_name')
Run Code Online (Sandbox Code Playgroud)