# RoR : Shortening this code

**URL:** <https://www.ruby-forum.com/t/ror-shortening-this-code/71239>\
**Category:** Ruby\
**Created:** [October 6, 2006, 11:30am UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239 "2006-10-06T11:30:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 6, 2006, 11:30am UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/1 "2006-10-06T11:30:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 6, 2006, 11:38am UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/2 "2006-10-06T11:38:59Z")

</div>

> Under Ruby On Rails, I would like to replace

This question is probably better asked at the RoR mailing list:  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 6, 2006, 11:40am UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/3 "2006-10-06T11:40:59Z")

</div>

Zouplaz wrote:

> 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:  
[Peak Obsession](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.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 6, 2006, 11:44am UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/4 "2006-10-06T11:44:59Z")

</div>

Zouplaz wrote:

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

You even have it again:

> Even statut =’#{:traite.to\_s}’" don’t work  
> ^ ^^

Hope that helps.

-Justin

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 6, 2006, 12:21pm UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/5 "2006-10-06T12:21:02Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 7, 2006, 12:21pm UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/6 "2006-10-07T12:21:56Z")

</div>

Zouplaz wrote:

> Hello,
> 
> Under Ruby On Rails, I would like to replace
> 
> 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.

David V.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 7, 2006, 7:23am UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/7 "2006-10-07T07:23:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [October 8, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/ror-shortening-this-code/71239/8 "2006-10-08T12:35:53Z")

</div>

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.
