Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql查询顺序问题_Sql_Ruby On Rails_Sqlite_Activerecord - Fatal编程技术网

sql查询顺序问题

sql查询顺序问题,sql,ruby-on-rails,sqlite,activerecord,Sql,Ruby On Rails,Sqlite,Activerecord,所以我在activerecord中得到了以下模型,我将使用Discussion.user\u回复\u组_discussions@userit返回最近回复的讨论,但如何修改private方法,以便在最新回复时间之前对返回的讨论进行排序 # Table name: discussions # # id :integer not null, primary key # title :string(255) not null

所以我在activerecord中得到了以下模型,我将使用Discussion.user\u回复\u组_discussions@userit返回最近回复的讨论,但如何修改private方法,以便在最新回复时间之前对返回的讨论进行排序

# 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\u by\u sql一起使用。但把它放在控制器里似乎很尴尬


有更好的解决办法吗?谢谢

这将根据最新讨论进行订购。更新时间:

如果要按comment.created_在排序,请使用以下查询:

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

这将根据最新讨论进行订购。更新时间:

如果要按comment.created_在排序,请使用以下查询:

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

谢谢那完全有效!问题是,我是rails新手,我不知道如何使用原始sql替换where方法:@randomor:如果您的问题没有解决,请编辑该问题,使其重新出现在问题列表中。Modified!谢谢你的精彩回答,我现在正在使用它,但不知道是否有更好的方法。@randomor:检查这个问题:@yercube:谢谢,但是如果用户在同一个讨论中发表了两条评论,这个解决方案会导致双重讨论问题,而你的查询通过使用MAX函数巧妙地避免了这个问题。我想在rails代码中不可能做到这一点。我将再等24小时,寻找更好的解决方案谢谢那完全有效!问题是,我是rails新手,我不知道如何使用原始sql替换where方法:@randomor:如果您的问题没有解决,请编辑该问题,使其重新出现在问题列表中。Modified!谢谢你的精彩回答,我现在正在使用它,但不知道是否有更好的方法。@randomor:检查这个问题:@yercube:谢谢,但是如果用户在同一个讨论中发表了两条评论,这个解决方案会导致双重讨论问题,而你的查询通过使用MAX函数巧妙地避免了这个问题。我想在rails代码中不可能做到这一点。我将再等24小时,寻找更好的解决方案