假设有模型article和模型comments,关系为
class Article < ApplicationRecord
has_many :comments
xxxx
end
# comments表里面有created_at用来标记评论时间
class Comments < ApplicationRecord
belongs_to :comment
xxxx
end
现在要将文章以最新评论时间排序,那么可以使用
Article.left_joins(:comments).order("comments.created_at desc")