# How to change all values for object

**URL:** <https://rubytalk.org/t/how-to-change-all-values-for-object/42544>\
**Category:** ruby-talk\
**Created:** [27 November 2007 11:50 UTC](https://rubytalk.org/t/how-to-change-all-values-for-object/42544 "2007-11-27T11:50:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bober](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Bober](https://rubytalk.org/u/Bober)\
**Post date:** [27 November 2007 11:50 UTC](https://rubytalk.org/t/how-to-change-all-values-for-object/42544/1 "2007-11-27T11:50:02Z")

</div>

Hello!  
I need change all values of Active Record Object

// Post - obiekt AR

@invo = Post.find(params[:id])  
@a = @invo.attributes

@a.each { |m, n|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@invo.m = "new\_value"  
}

But, I see "undefined method `m=' "

what is wrong?  
how can I change values?

---

<div class="post-metadata">

**Author:** ![Alex\_Fenton](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Alex\_Fenton](https://rubytalk.org/u/Alex_Fenton)\
**Post date:** [27 November 2007 12:05 UTC](https://rubytalk.org/t/how-to-change-all-values-for-object/42544/2 "2007-11-27T12:05:05Z")

</div>

Bober wrote:

> @invo = Post.find(params[:id])  
> @a = @invo.attributes
> 
> @a.each { |m, n|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@invo.m = "new\_value"  
> }
> 
> But, I see "undefined method `m=' "

Because you are calling the literal method "m=", which doesn't exist. You want instead, something like (untested, I don't use ActiveRecord).

@invo.send("#{m}=", "new\_value")

alex

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [27 November 2007 13:54 UTC](https://rubytalk.org/t/how-to-change-all-values-for-object/42544/3 "2007-11-27T13:54:59Z")

</div>

From the docs...

[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001069](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001069)

...it appears that @a would be a Hash. If that is correct, you have to  
use the subscript operator (or #store) to change the items...

@invo = Post.find(params[:id])  
@a = @invo.attributes

@a.each { |m, n|  
&nbsp;&nbsp;@invo[m] = "new\_value"  
&nbsp;&nbsp;# ^^^  
}

Look at the ruby documentation for the Hash class...

[http://www.ruby-doc.org/core/classes/Hash.html](http://www.ruby-doc.org/core/classes/Hash.html)

Regards,  
Jordan

> **···**
>
> On Nov 27, 5:48 am, Bober \<sebastian.bobrow...@gmail.com\> wrote:
> 
> > Hello!  
> > I need change all values of Active Record Object
> > 
> > // Post - obiekt AR
> > 
> > @invo = Post.find(params[:id])  
> > @a = @invo.attributes
> > 
> > @a.each { |m, n|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@invo.m = "new\_value"
> > 
> > }
> > 
> > But, I see "undefined method `m=' "
> > 
> > what is wrong?  
> > how can I change values?

---

<div class="post-metadata">

**Author:** ![Bober](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Bober](https://rubytalk.org/u/Bober)\
**Post date:** [27 November 2007 18:20 UTC](https://rubytalk.org/t/how-to-change-all-values-for-object/42544/4 "2007-11-27T18:20:00Z")

</div>

> Look at the ruby documentation for the Hash class...
> 
> [http://www.ruby-doc.org/core/classes/Hash.html](http://www.ruby-doc.org/core/classes/Hash.html)

Thank you
