在Rails 3.2中,我一直在尋找一種方法來遍歷before_add回調中對象的關聯。在Rails 3中訪問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,但我可能會錯過一些東西。有任何想法嗎?
它也implossible從您正在尋找這樣那樣的穿越使得其很難用戶開會就知道了。你不能說self.metting.user,因爲用戶擁有並且屬於許多會議,這意味着你可以在不知道id的情況下從會議中獲得特定用戶。 – 2013-03-26 16:29:44