Note, Rails will take the symbol and use to_yaml instead of to_s when
it saves the record, so you should explictly change it to a string
yourself before passing it in the update hash.
Mouvement.update(params[:id], statut =’#{:traite}’")
Thank you
I can’t tell you exactly, but I can give you a hint. It is converting
:traite to a string. But, you have a mistype somewhere, because you have
an uneven number of double quotes. I’m guessing that when you
pasted/wrote the code in your email, this was obscured.
mouvement = Mouvement.find(params[:id])
mouvement.statut = :traite
mouvement.save!
I like this variant better. The whole point of ActiveRecord is that you
handle the database tables like objects, and the use of #update_attribute makes the code read more like SQL generator calls
instead of ORM use. YMMV.
I guess it’s personal preference. There’s not really any huge
difference between the two methods. It all depends on what the context
is – update() returns the object, and update_attribute() returns
true/false depending on whether the update succeeded or not (minus
validation checks).
ActiveRecord is an ORM that doesn’t feel like an ORM. The fact that
things like #update_attribute and #find_by_sql exist are part of the
reason that I like ActiveRecord. It’s a light wrapper around my
database that lets me get down and dirty with SQL if need to (which
happens quite a bit).
Maybe I’m too pragmatic, but I definitely prefer writing one line of
code to writing 3.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.