简体   繁体   English

Rails如何更新所有记录的列为零的记录?

[英]Rails how to update records where column is nil for all records?

I want to set udtraekkes (datetime) to NULL for all those records where udtraekkesinfo is NOT equal NULL or NIL. 对于udtraekkesinfo不等于NULL或NIL的所有记录,我想将udtraekkes(datetime)设置为NULL。 My model name is Konkurrencer. 我的型号名称是Konkurrencer。

How do I update alle the konkurrencers records in the rails console? 如何更新rails控制台中的konkurrencers记录?

I think you need update_all method. 我认为你需要update_all方法。 Documentation 文档

In Rails 4.0.13, I tested with this syntax might it would do same for you in rails 3. 在Rails 4.0.13中,我使用这种语法测试它可能会在rails 3中为你做同样的事情。

Model.where(conditions).update_all(:column_name=> nil)
where conditions are filtered condition to fetch the each record and update_all will operate on those record, you can update one or more column based on your requirement. 

for your requirement code would be. 对于您的需求代码将是。

Konkurrencer.where(:udtraekkes=> nil).update_all(:udtraekkes=> nil)

Konkurrencer.update_all({udtraekkes:nil},“udtraekkesinfo IS NOT NULL”)

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

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