jacoulter
jacoulter

Reputation: 740

Rails: How to update model records from console?

I need to convert values stored in my Rails 3.0 application's database table from pounds to kilograms. This is a one-time task.

Is this something I could do from the console? If so, how? The values are stored in my weights table in the weight_entry column.

I know it should look something like:

Weights.each do |weight|
  weight.weight_entry = weight.weight_entry * 0.45
  weight.save
end

I don't know how to initialize the Weights constant though.

Thanks for you help!

Upvotes: 0

Views: 3934

Answers (2)

Ritesh Choudhary
Ritesh Choudhary

Reputation: 782

Try This

rails c

Weight.all.each{ |weight_obj| weight_obj.update_attributes(:weight_entry => (weight_obj.weight_entry * 0.45) ) }

Upvotes: 1

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

rails c

weights = Weight.all

weights.each do |weight|..

Upvotes: 5

Related Questions