简体   繁体   English

Rails:如何从控制台更新模型记录?

[英]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. 我需要将Rails 3.0应用程序的数据库表中存储的值从磅转换为公斤。 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. 这些值存储在我的体重表的weight_entry列中。

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! 感谢您的帮助!

rails c

weights = Weight.all

weights.each do |weight|..

Try This 尝试这个

rails c 导轨c

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM