我遇到了ActiveRecord::Observer
记录各种站点的现有模型Activities
的User
问题。一切都很好,直到我尝试使用与其他模型相同的模型来上课observe
。考虑一下:User
Activity
observe
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :item, :polymorphic => true
end
class ActivityObserver < ActiveRecord::Observer
observe :billing, :call, :vanity_number
end
class User < ActiveRecord::Base
has_many :comments
has_many :activities
end
class Comment < ActiveRecord::Base
belongs_to :user
has_many :activities, :as => :item
end
以上工作正常。查询User.activities
将返回Comment
Activities
. 一旦我添加:user
到observe
方法中并将模型ActivityObserver
更改为,将只返回与该实例相关的活动而不是任何has_many :activities
User
has_many :activities, :as => :item
User.activities
User
Comments
为什么是这样?我能做些什么来让它按预期工作?