【发布时间】:2011-09-30 08:32:39
【问题描述】:
所以,我有一个多态的Notification 模型,我希望能够过滤掉属于notifiable_type Comment 的通知,其中comment.user == current_user。换句话说,我想要所有的通知记录——除了那些引用当前用户创建的 cmets 的记录。
class Notification
belongs_to :notifiable, :polymorphic => true
scope :relevant, lambda { |user_id|
find(:all, :conditions => [
"notifiable_type != 'Comment' OR (notifiable_type = 'Comment' AND " <<
"comments.user_id != ?)",
user_id ],
:include => :comments
)
}
end
我不明白我需要做什么才能访问 cmets?我需要告诉 ActiveRecord 在notifiable_id 上加入评论模型。
【问题讨论】:
-
你从上面得到了什么结果?例如。如果你打电话给
Notification.relevant.first.comments,你会得到什么? -
一个错误..“ActiveRecord::ConfigurationError: Association named 'cmets' is not found;也许你拼错了?”..因为通知没有 cmets..它有一个“通知”可以是评论,也可以是其他模型实例。
-
但是你对
:include => :comments说你不应该说:include => :notifiables或类似的东西吗?评论是否需要通知? -
with :include => :notifiable, 我得到:“ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :notifiable”.. 因此我的问题和我的问题..
标签: ruby-on-rails ruby-on-rails-3 activerecord