迭代named_scope内的has_many集合

发布于 2024-10-24 04:40:27 字数 440 浏览 2 评论 0原文

这是我的模型:

class Message < ActiveRecord::Base
  has_many :comments

  attr_accessible :read #bool

  def unread_comments?
    comments.each { |comment| return true unless comment.read?}

    false
  end
end

class Comment < ActiveRecord::Base
  belongs_to :message

  attr_accessible :read #bool
end

这是我想要做的:我想在消息中创建一个名为 unread 的named_scope,如果消息的任何评论或消息本身未读,它基本上返回 true未读。我有办法做到这一点吗?

Here are my models:

class Message < ActiveRecord::Base
  has_many :comments

  attr_accessible :read #bool

  def unread_comments?
    comments.each { |comment| return true unless comment.read?}

    false
  end
end

class Comment < ActiveRecord::Base
  belongs_to :message

  attr_accessible :read #bool
end

Here is what I am looking to do: I'd like to create a named_scope in Message, called unread that basically returns true if any of the message's comments are unread or the message itself is unread. Is there a way I can do this?

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

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

发布评论

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

评论(3

如何视而不见 2024-10-31 04:40:27
class Message < AR::Base
  ...
  def unread?
    !self.read && !self.comments.all?(&:read)
  end
end
class Message < AR::Base
  ...
  def unread?
    !self.read && !self.comments.all?(&:read)
  end
end
冷月断魂刀 2024-10-31 04:40:27

这个怎么样:

named_scope :unread, :conditions => ['messages.read = ? OR comments.read = ?', 
                                      false, false],
                     :include => :comments

How about this:

named_scope :unread, :conditions => ['messages.read = ? OR comments.read = ?', 
                                      false, false],
                     :include => :comments
白首有我共你 2024-10-31 04:40:27

为什么命名范围?你可以这样做

def has_unread
  !self.read || unread_comments?
end

,或者,如果它需要是一个类方法,

def self.has_unread(message)
  msg = Message.find(message.id)
  msg.read == false && msg.unread_comments?
end

但是你真的想要使用命名范围吗,你会想要这样的东西:

scope :get_unreads, lambda {|message|
  { :joins => :comments,
    :conditions => {:comments => {:message_id => message.id}, :message => ['read',0]},
    :select     => "DISTINCT `clients`.*" # kill duplicates
  }
}
def self.has_unread?(message)
  self.get_unreads(message).exists? && self.find(message.id).read
end

Why the named scope? You could just do

def has_unread
  !self.read || unread_comments?
end

or, if it needs to be a class method

def self.has_unread(message)
  msg = Message.find(message.id)
  msg.read == false && msg.unread_comments?
end

Is you really do want to use named scopes though, you'll want something like this:

scope :get_unreads, lambda {|message|
  { :joins => :comments,
    :conditions => {:comments => {:message_id => message.id}, :message => ['read',0]},
    :select     => "DISTINCT `clients`.*" # kill duplicates
  }
}
def self.has_unread?(message)
  self.get_unreads(message).exists? && self.find(message.id).read
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文