[英]How to sort by an associated column in this active record query
我需要一些按关联模型排序的查询。 我有一个要获取的客户列表,我需要确保按照收到的最后一封邮件对客户列表进行排序。 以下是当前查询:
@open_customers = @company.customers.open.includes(:last_received_message)
@your_customers = current_user.customers.includes(:last_message)
@sidebar_customers = current_company.customers.open_or_claimed.
where("user_id = ? OR user_id IS NULL", current_user.id).
order(aasm_state: :asc).includes([:last_received_message, :user])
这是三个单独的查询。
@open_customers需要按last_received_message的created_at排序@your_customers需要按last_message的created_at排序@sidebar_customers需要按last_received_message的created_at排序
谢谢!
您可以尝试在此处查看第14节: 范围 : http : //guides.rubyonrails.org/active_record_querying.html
该网站上还有一门名为Rails 4 Outlaws的课程,在教学范围上做得很好: https : //www.codeschool.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.