重构一段时间后创建的父级子级的查找

发布于 2024-10-08 01:34:57 字数 728 浏览 7 评论 0原文

我试图找到在日期时间之后创建的任何不是当前用户发出的评论。

起初我这样做了..

current_user.comments.find(:all, :conditions=>["created_at > ? AND user_id != ?",
                            current_user.last_checked_mail, current_user])

但这里的问题是,因为它从用户模型开始,所以它只能找到该用户专门发表的评论。

因此,我开始搜索与用户相关的所有评论,以及这些评论的子项,只要它们的 user_id 不是 current_user

Comment.find(:all, :conditions => ["user_id = ?", current_user]).select { |c|
  c.comments.find(:all, :conditions => ["user_id != ?", current_user])  
}

但似乎仍然绘制与 current_user 相关的每一条评论,而不是它们的子项。

我的评论模型:

belongs_to  :commentable, :polymorphic => true
has_many    :comments, :as => :commentable

I am trying to find any comments created after a datetime that were not made by the current user.

At first I did this..

current_user.comments.find(:all, :conditions=>["created_at > ? AND user_id != ?",
                            current_user.last_checked_mail, current_user])

but the problem here was that because it begins with the user model, its only finding comments exclusively made by that user.

So instead, I began searching for all comments associated with the user, and those comments children so long as their user_id is not the current_user

Comment.find(:all, :conditions => ["user_id = ?", current_user]).select { |c|
  c.comments.find(:all, :conditions => ["user_id != ?", current_user])  
}

But it seems that still draws every single comment related to the current_user, and not their children.

My Comment Model :

belongs_to  :commentable, :polymorphic => true
has_many    :comments, :as => :commentable

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

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

发布评论

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

评论(3

一身软味 2024-10-15 01:34:57

那么你的评论模型中有父母关系吗?用那个!

class Comment < ActiveRecord::Base

   belongs_to :user
   belongs_to :parent, :class_name => "Comment"

   def self.after(date)
     where "created_at > ?", date
   end

   def self.replies_for(comments)
     where :parent_id => comments.map(&:id)
   end

   def self.exclude_user(user_id)
     where "user_id != ?", user_id
   end

 end

 class User < ActiveRecord::Base

    has_many :comments

    def new_comments
      comments.after(last_check_mail)
    end

    def new_responses
      Comment.replies_for(new_comments).after(last_check_mail).exclude_user(id)
    end

 end

 current_user.new_responses

附言。这是针对 Rails 3 的。

So you have a parent relation in your comment model? Use that!

class Comment < ActiveRecord::Base

   belongs_to :user
   belongs_to :parent, :class_name => "Comment"

   def self.after(date)
     where "created_at > ?", date
   end

   def self.replies_for(comments)
     where :parent_id => comments.map(&:id)
   end

   def self.exclude_user(user_id)
     where "user_id != ?", user_id
   end

 end

 class User < ActiveRecord::Base

    has_many :comments

    def new_comments
      comments.after(last_check_mail)
    end

    def new_responses
      Comment.replies_for(new_comments).after(last_check_mail).exclude_user(id)
    end

 end

 current_user.new_responses

PS. This is for Rails 3.

心的憧憬 2024-10-15 01:34:57

尝试:

Comment.find(:all, :conditions => ["created_at > ? AND user_id != ?", current_user.last_checked_mail, current_user])

Try:

Comment.find(:all, :conditions => ["created_at > ? AND user_id != ?", current_user.last_checked_mail, current_user])
别想她 2024-10-15 01:34:57

试试这个:

Comment.all(:conditions => ["user_id != ? AND parent_id = ? AND created_at > ?",
           current_user.id, current_user.id, current_user.last_checked_mail])

更好的解决方案是在 Comment 模型上创建一个named_scope。

class Comment

  named_scope :other_comments, lambda {|u| { :conditions => 
                ["user_id != ? AND parent_id = ? AND created_at > ?",
                  u.id, u.id, u.last_checked_mail ] }}
end

现在您可以通过以下方式获取其他评论:

Comment.other_comments(current_user)

编辑 1
基于多态关联的更新答案:

class Comment
  def self.other_comments(u)
    Comment.all(
      :joins => "JOIN comments A 
                 ON   A.commentable_type = 'User' AND 
                      A.commentable_id = #{u.id} AND 
                      comments.commentable_type = 'Comment' AND 
                      comments.commentable_id = A.id",

      :conditions => ["comments.user_id != ? AND comments.created_at > ?",
             u.id, u.last_checked_mail]
    )
  end
end

现在您可以获得其他评论:

Comment.other_comments(current_user)

Try this:

Comment.all(:conditions => ["user_id != ? AND parent_id = ? AND created_at > ?",
           current_user.id, current_user.id, current_user.last_checked_mail])

Better solution is to create a named_scope on the Comment model.

class Comment

  named_scope :other_comments, lambda {|u| { :conditions => 
                ["user_id != ? AND parent_id = ? AND created_at > ?",
                  u.id, u.id, u.last_checked_mail ] }}
end

Now you can get others comments as:

Comment.other_comments(current_user)

Edit 1
Updated answer based for Polymorphic associations:

class Comment
  def self.other_comments(u)
    Comment.all(
      :joins => "JOIN comments A 
                 ON   A.commentable_type = 'User' AND 
                      A.commentable_id = #{u.id} AND 
                      comments.commentable_type = 'Comment' AND 
                      comments.commentable_id = A.id",

      :conditions => ["comments.user_id != ? AND comments.created_at > ?",
             u.id, u.last_checked_mail]
    )
  end
end

Now you can get others comments as:

Comment.other_comments(current_user)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文