Update_all或update_attribute不会更新该列 [英] Update_all or update_attribute doesn't update the column
问题描述
我有一个状态报告,用户可以通过电子邮件发送该状态报告,我想在完成传递操作后将列:sent_mail更新为true.
I have a status report a user can send by email and I want to update a column :sent_mail to true, after the deliver action is completed.
def send_status
date = Date.today
reports = current_user.reports.for_date(date)
ReportMailer.status_email(current_user, reports, date).deliver
reports.update_all(sent_mail: true)
end
和表类
AddSentMailToReports < ActiveRecord::Migration
def change
add_column :reports, :sent_mail, :boolean, default: false
end
end
但是,在控制台中,sent_mail仍然设置为false.有什么想法为什么不起作用? 谢谢!
However, in console, sent_mail is still set to false.Any ideas why this doesn't work? Thanks!
推荐答案
这是因为update_all直接将更新发送到数据库-它不会更新内存中的报表模型.如果您在运行当前版本后检查数据库,则会看到记录已更新.
This is because update_all sends an update directly to the database - it won't update the report models you have in memory. If you check the database after running your current version, you'll see that the records have been updated.
您需要在每个报告上调用重新加载",以从数据库中获取更新的版本,或者
You need to call 'reload' on each report to get the updated version from the database or
reports.map(&:reload)
一次性完成所有操作.
这篇关于Update_all或update_attribute不会更新该列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!