我有一个模型,当它实例化一个对象时,还会使用相同的用户ID创建另一个对象。在rails中绕过attr_accessible/protected
class Foo > ActiveRecord::Base
after_create: create_bar
private
def create_bar
Bar.create(:user_id => user_id #and other attributes)
end
end
在Bar.rb中,我有attr_protected以保护它免受黑客攻击。
class Bar > ActiveRecord::Base
attr_protected :user_id, :created_at, :updated_at
end
目前的情况是,我似乎无法不创建一个新的酒吧对象要么禁用attr_protected或具有Bar对象的user_id一片空白......
我怎样才能让酒吧对象接受:来自foo的user_id属性不会失去对attr_protected的保护?
This Works!谢谢jonnii。除了在我的情况下,我做了bar = user.bar.build(...参数) – 2010-01-22 21:09:33