RoR : Shortening this code

Z

Zouplaz

Hello,

Under Ruby On Rails, I would like to replace

mouvement = Mouvement.find(params[:id])
mouvement.statut = :traite
mouvement.save!

By

Mouvement.update(params[:id], statut ='#{:traite}'")

But this error occurs : undefined method `stringify_keys!' for "statut =
traite":String


I don't understand the meaning of the error. Why does Ruby doesn't knows
how to convert the symbol :traite to a string representation ?

Even statut ='#{:traite.to_s}'" don't work

Any help ?

Thank you
 
E

eden

Zouplaz said:
Mouvement.update(params[:id], statut ='#{:traite}'")

This isn't valid, did you mean? (note, the ")
Mouvement.update(params[:id], "statut ='#{:traite}'")

The problem is not that Ruby can't stringify :traite, instead the
problem is that you've not read the RoR docs on update() well enough.
Please see:
http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000864

If you've read this, you'd change your code to look like this:
Mouvement.update(params[:id], { :statut => 'traite' })

Or, better yet
Mouvement.find(params[:id]).update_attribute:)statut, 'traite')

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.
 
C

Collins, Justin

------_=_NextPart_001_01C6E92B.DCD2A216
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


Hello,

Under Ruby On Rails, I would like to replace

mouvement =3D Mouvement.find(params[:id])
mouvement.statut =3D :traite
mouvement.save!

By

Mouvement.update(params[:id], statut =3D'#{:traite}'")

But this error occurs : undefined method `stringify_keys!' for "statut = =3D traite":String


I don't understand the meaning of the error. Why does Ruby doesn't =
knows how to convert the symbol :traite to a string representation ?
Even statut =3D'#{:traite.to_s}'" don't work

Any help ?

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.update(params[:id], statut =3D'#{:traite}'")
^ ^^

You even have it again:
Even statut =3D'#{:traite.to_s}'" don't work
^ ^^

Hope that helps.

-Justin

------_=_NextPart_001_01C6E92B.DCD2A216--
 
Z

Zouplaz

le 06/10/2006 11:40, eden nous a dit:
The problem is not that Ruby can't stringify :traite, instead the
problem is that you've not read the RoR docs on update() well enough.

You're right ! I read it the wrong way ;-)
If you've read this, you'd change your code to look like this:
Mouvement.update(params[:id], { :statut => 'traite' })

Or, better yet
Mouvement.find(params[:id]).update_attribute:)statut, 'traite')

Why is the second solution the better one ?

Thank you !
 
E

eden

Why is the second solution the better one ?

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).
 
D

David Vallner

--------------enigA6B85573E7103654F9F86552
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hello,
=20
Under Ruby On Rails, I would like to replace
=20
mouvement =3D Mouvement.find(params[:id])
mouvement.statut =3D :traite
mouvement.save!
=20

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.

David Vallner


--------------enigA6B85573E7103654F9F86552
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFJ3+by6MhrS8astoRAp4RAJ47/vdD70FYlrnCcMGbzs0hWcXjhgCeLaB8
6SA3LDmizL8bllFtrQcIK7U=
=afhv
-----END PGP SIGNATURE-----

--------------enigA6B85573E7103654F9F86552--
 
E

eden li

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.

David said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top