How can I specifiy attr_accessible for a nested attributes?
-
30-06-2021 - |
题
From the docs, I see that if I have a model:
User has_many Addresses
that I can do this:
class User < AR::Base
attr_accessible :addresses_attributes
accepts_nested_attributes_for :addresses
end
that I should be able to set those attributes from User. But I can't. Do I also need to add an "attr_accessible" callout in the Address model?
解决方案
Yes, you have to set attr_accessible on the Address model as well.
When ActiveRecord initializes an instance using a hash of attributes it will throw an exception when an attribute which has not been marked by attr_accessible is a part of the hash keys. Since in associations ActiveRecord objects are initialized with a hash of attributes there's no escaping from stating those attributes inside the Address model with attr_accessible.
不隶属于 StackOverflow