[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to change all values for object

Bober

11/27/2007 11:48:00 AM

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|
@invo.m = "new_value"
}

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

what is wrong?
how can I change values?
3 Answers

Alex Fenton

11/27/2007 12:04:00 PM

0

Bober wrote:

> @invo = Post.find(params[:id])
> @a = @invo.attributes
>
> @a.each { |m, n|
> @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

MonkeeSage

11/27/2007 1:53:00 PM

0

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|
> @invo.m = "new_value"
>
> }
>
> But, I see "undefined method `m=' "
>
> what is wrong?
> how can I change values?

From the docs...

http://api.rubyonrails.com/classes/ActiveRecord/Base.ht...

....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|
@invo[m] = "new_value"
# ^^^
}

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

http://www.ruby-doc.org/core/classes...

Regards,
Jordan

Bober

11/27/2007 6:16:00 PM

0


>
> Look at the ruby documentation for the Hash class...
>
> http://www.ruby-doc.org/core/classes...

Thank you