如何在控制台中将表Notification.email更新为全真?
在控制台中,我想遍历表中的所有记录并设置email = true。
想法?
发布于 2011-10-08 05:49:05
你在找update_all。See doc。
注意,这种方式不会触发回调。
发布于 2011-10-08 05:55:32
这应该是可行的
Notification.all.each do |n| n.update_attribute(:email, true); end编辑:从bricker定制:
Notification.all.each { |n| n.update_attribute(:email, true) }发布于 2014-06-23 21:43:12
您可以使用:
Notification.update_all(email: true)如果你在更新的时候另外有一个条件,你应该使用:
Notification.find_each { |n| n.email = (n.id > 100) ? true : false }对于大型数据库表来说,使用Something.all不是一个好主意。
https://stackoverflow.com/questions/7693127
复制相似问题