这是我的模型:
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 。有什么办法可以做到这一点吗?
最佳答案
class Message < AR::Base
...
def unread?
!self.read && !self.comments.all?(&:read)
end
end
关于ruby-on-rails - 迭代named_scope内的has_many集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5330607/