[英]Update_all or update_attribute doesn't update the column
我有一個用戶可以通過電子郵件發送的狀態報告,我希望在交付操作完成后更新列:sent_mail為true。
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。任何想法為什么這不起作用? 謝謝!
這是因為update_all直接向數據庫發送更新 - 它不會更新您在內存中的報表模型。 如果在運行當前版本后檢查數據庫,則會看到記錄已更新。
您需要在每個報告上調用“reload”以從數據庫中獲取更新版本或
reports.map(&:reload)
一次完成所有這些。
如果update_all或update_attribute不起作用,您可以使用它
reports.update_attributes(:sent_mail => true)
Ref update_all
, update_attributes
和update_attribute
更改
reports.update_all(sent_mail: true)
至
reports.update_attribute('sent_mail', true)
update_attribute和update_attributes update_attribute vs update_attributes
之間存在差異
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.