I’m trying to do update_attributes with Mongoid in a Rails 3 application
The problem I’m running into is this:
Let’s say my collection has the following fields:
{“Name” : “foo”, “email” : “bar” }
Here’s the scenario
if I do this:
@person = Person.where(:Name => "foo", :_id = "some_id")
and then I do this:
@person.update_attributes(:Name => "baba-fooka", :Last_Name => "baba-bara")
The line of code above updates the record in mongodb, but also adds a new field to the document.
How can I use the update_attributes method with a validation which doesn’t allow inserting fields which don’t already exist
It sounds like what you want is allow_dynamic_fields set to false in the mongoid config file. Dynamic fields is on by default which allows attributes to get set and persisted on the document even if a field was not defined for them.
Go into
config/mongoid.ymlunder options setallow_dynamic_fields: false. It should already be there but commented out and set to true. Make sure it says false.