sql查询顺序问题
因此,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将根据最新的
discussions.updated_at
时间排序:如果您想按
comment.created_at
排序,请使用以下查询:This will order according to latest
discussions.updated_at
time:If you want to order by
comment.created_at
, use this query: