how to change all values for object

B

Bober

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?
 
A

Alex Fenton

Bober said:
@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
 
M

MonkeeSage

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

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

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

Regards,
Jordan
 

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

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top