How can I tell Ruby (Rails) to ignore protected variables which are present when mass-assigning?
class MyClass < ActiveRecord::Base
attr_accessible :name, :age
end
Now I will mass-assign a hash to create a new MyClass
.
MyClass.create!({:name => "John", :age => 25, :id => 2})
This will give me an exception:
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: id
I want it to create a new MyClass
with the specified (unprotected) attributes and ignore the id
attribute.
On the side note: How can I also ignore unknown attributes. For example, MyClass
doesn't have a location
attribute. If I try to mass-assign it, just ignore it.