I just started using Datamapper.
I am trying to update an object. I get the object/model using its id:
u1 = User.get(1)
u1.name = "xyz"
u1.update
which throws a error/raises an exception. I tried again:
u1 = User.get(1)
and after that:
u1.update({:name => "xyz"})
will throw false and dirty? returns true.
After that any call to update would fail saying it is dirty.
I can do a save by:
u1.name = "xyz"
u1.save
Here are my questions:
- What should I be using: Save or update?
- Should I say
get(id)even to just change one field? - When should I use
update? What is the syntax:user.update({ ....})oruser.name = "xyz"; user.update? - What is
dirty?, and is it once I make a object dirty do I have to
get the object fresh from the database to the variable?
When you fetched a resource from the db and then changed its attributes then the resource becomes ‘dirty’. This means that the resource is loaded into memory and its state has changed and changes can be persisted in the db.
You use
#saveto persist changes made to a loaded resource and you use#updatewhen you want to immediately persist changes without changing resource’s state to ‘dirty’. Here’s an example session: