在 Rails 3.2 中,我一直在寻找一种在 before_add 回调中遍历对象关联的方法。
所以基本上我的用例是:
class User < ActiveRecord::Base
has_and_belongs_to_many :meetings
end
class Meeting < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :comments, :before_add => :set_owner_id
end
class Comment < ActiveRecord::Base
belongs_to :meeting
end
def set_owner_id(child)
child.owner_id = <<<THE USER ID for @user >>>
end
我正在用户的上下文中创建评论:
@user.meetings.first.comments.create
如何从 before_add 回调中遍历关联以发现 @user 的 id?我想在模型级别设置它。我一直在查看 proxy_association,但我可能遗漏了一些东西。有任何想法吗?