Searched for it, but found nothing, so i open a new topic
I think it is very basic stuff…
My model has a validation rule like this:
validates_presence_of :title
If i try to save the model (@mymodel.save) with an empty title-formfield
i get an error. hooray! that’s what i want!
okay i insert a valid title and everything is fine.
now i want to update the model (@mymodel.update) and - SURPRISE! - now
i can update title to an empty string “”! Why?
Even if i have this in my rule
validates_presence_of :title, :on => :update
i can update my model without an error and have an empty string for the
title in my database.
i dont get it. the API says:
update(id, attributes)
Finds the record from the passed id, instantly saves it with the passed
attributes (if the validation permits it), and returns it.
It looks like you’re confusing Model.update with @model.update_attributes. Model.update(id, attributes) should be
called on the class, not an instance.
Person.update(1, { :name => ‘Jonathan’ })
Is equivalent to:
p = Person.find(1)
p.update_attributes({:name => ‘Jonathan’})
Surprisingly, p.update actually saves the record without looking at
the validation… that could be classed as misleading at best. You
should use update_attributes, not update.
ActiveRecord::Base#update is private in base.rb, but locking.rb does
not preserve its private status. Looks like a small bug, I’ll file it
tomorrow.
Oh damn, that is indeed misleading. I didnt realize that update' is a private method. well, i usedupdate’ everywhere in my application to update stuff.
Thanks for your help! I’m off to rewrite some code …
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.