【发布时间】:2018-01-08 10:41:37
【问题描述】:
在 rails 5 和 postgresql 中,我想根据 created_at: :desc 对列表进行排序。
模型结构是这样的, 模型/review.rb
has_many :comments
has_many :commented_users, :through=>:comments, :source=>:user
在视图中,我想根据created_at (DESC) 日期显示用户列表,该字段来自comments 表
控制器查询就像,
review = Review.find_by_id(params[:id])
comments = review.commented_users
如果我为review.commented_users 提供order,那么显然它将对User 进行排序,我如何根据Comment 对用户进行排序?
审核字段是,
=> #<Review id: nil, user_id: nil, content: nil, created_at: nil, updated_at: nil>
评论字段是,
=> #<Comment id: nil, review_id: nil, user_id: nil, created_at: nil, updated_at: nil>
用户字段是,
=> #<User id: nil, email: "", created_at: nil, updated_at: nil, first_name: nil, last_name: nil, inactive: false, password_updated_at: nil, active: true, reviews_count: 0, notification_seen_at: nil, deleted_at: nil, status: "pending">
请帮我解决这个排序问题,我是postgresql的新手
【问题讨论】:
-
这里又是
id将采用User模型而不是Comment。 -
关联有问题
-
你能显示你的评论字段和关系吗?
-
问题已更新
-
@ShruthiR 我认为
commented_users也会返回users数组,对吗?
标签: ruby-on-rails ruby postgresql sorting ruby-on-rails-5