sql查询顺序问题

发布于 2024-11-01 14:43:20 字数 1567 浏览 0 评论 0原文

因此,我在 activerecord 中有以下模型,我将使用 Discussion.user_replied_group_discussions(@user) 它会返回最近回复的讨论,但是如何修改私有方法以按以下方式对返回的讨论进行排序最晚回复时间?

# Table name: discussions
#
#  id               :integer         not null, primary key
#  title            :string(255)     not null
#  content          :text(255)       not null
#  created_at       :datetime
#  updated_at       :datetime
#  user_id          :integer
#  discussable_id   :integer
#  discussable_type :string(255)
class Discussion < ActiveRecord::Base
  has_many    :comments, :as => :commentable, :dependent => :destroy
  #
  #this is the scope that I am having trouble with:
  #
  scope :user_replied_group_discussions, lambda {|user| replied_by(user) }

  private
    def self.replied_by(user)
      discussion_ids = %(SELECT commentable_id FROM comments 
                     WHERE commentable_type = 'Discussion' AND user_id = :user_id)
      where("discussable_type = 'Group' AND id IN (#{discussion_ids}) ", 
           { :user_id => user })
    end
end

# Table name: comments
#
#  id               :integer         not null, primary key
#  content          :text
#  created_at       :datetime
#  updated_at       :datetime
#  commentable_id   :integer
#  commentable_type :string(255)
#  user_id          :integer
#

class Comment < ActiveRecord::Base

  belongs_to :commentable, :polymorphic => true
  belongs_to :user

end

更新: @ypercube 提供的 sql 查询有所帮助,我现在将其与 find_by_sql 一起使用。但将其放入控制器中似乎很尴尬。

有更好的解决方案吗?谢谢!

So I've got the follow model in activerecord, I will use Discussion.user_replied_group_discussions(@user)it returns the recent replied discussions alright, but how do I modify the private method to order the returned discussions by latest reply time?

# Table name: discussions
#
#  id               :integer         not null, primary key
#  title            :string(255)     not null
#  content          :text(255)       not null
#  created_at       :datetime
#  updated_at       :datetime
#  user_id          :integer
#  discussable_id   :integer
#  discussable_type :string(255)
class Discussion < ActiveRecord::Base
  has_many    :comments, :as => :commentable, :dependent => :destroy
  #
  #this is the scope that I am having trouble with:
  #
  scope :user_replied_group_discussions, lambda {|user| replied_by(user) }

  private
    def self.replied_by(user)
      discussion_ids = %(SELECT commentable_id FROM comments 
                     WHERE commentable_type = 'Discussion' AND user_id = :user_id)
      where("discussable_type = 'Group' AND id IN (#{discussion_ids}) ", 
           { :user_id => user })
    end
end

# Table name: comments
#
#  id               :integer         not null, primary key
#  content          :text
#  created_at       :datetime
#  updated_at       :datetime
#  commentable_id   :integer
#  commentable_type :string(255)
#  user_id          :integer
#

class Comment < ActiveRecord::Base

  belongs_to :commentable, :polymorphic => true
  belongs_to :user

end

UPDATE:
The sql query provided by @ypercube helped, I'm now using it with find_by_sql. But it seems awkward putting that in the controller.

Is there a better solution? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吐个泡泡 2024-11-08 14:43:21

这将根据最新的 discussions.updated_at 时间排序:

where("discussable_type = 'Group' AND id IN (#{discussion_ids}) 
order by updated_at DESC ",

如果您想按 comment.created_at 排序,请使用以下查询:

SELECT d.*
     , MAX(c.created_at) AS lastCommentTime
FROM discussions d
  JOIN comments c
    ON d.id = c.commentable_id
WHERE c.commentable_type = 'Discussion'
  AND c.user_id = userIDtoCheck
GROUP BY d.id
ORDER BY lastCommentTime DESC        <--- latest first

This will order according to latest discussions.updated_at time:

where("discussable_type = 'Group' AND id IN (#{discussion_ids}) 
order by updated_at DESC ",

If you want to order by comment.created_at, use this query:

SELECT d.*
     , MAX(c.created_at) AS lastCommentTime
FROM discussions d
  JOIN comments c
    ON d.id = c.commentable_id
WHERE c.commentable_type = 'Discussion'
  AND c.user_id = userIDtoCheck
GROUP BY d.id
ORDER BY lastCommentTime DESC        <--- latest first
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文