1

ユーザーが電子メールで送信できるステータス レポートがあり、配信アクションが完了した後で、列 :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 に設定されています。これが機能しない理由はありますか? ありがとう!

4

3 に答える 3

6

これは、update_all がデータベースに直接更新を送信するためです。メモリ内にあるレポート モデルは更新されません。現在のバージョンを実行した後にデータベースを確認すると、レコードが更新されていることがわかります。

データベースから更新されたバージョンを取得するには、各レポートで「reload」を呼び出す必要があります。

reports.map(&:reload)

それらすべてを一度に行うこと。

于 2012-09-26T09:47:27.800 に答える
0

update_allまたはupdate_attributeが機能しない場合は、これを使用できます

reports.update_attributes(:sent_mail => true)
于 2012-09-26T09:41:38.253 に答える
-1

参照update_allupdate_attributesおよびupdate_attribute

変化する

reports.update_all(sent_mail: true)

reports.update_attribute('sent_mail', true)

update_attribute と update_attributes には違いがありますupdate_attribute vs update_attributes

于 2012-09-26T09:36:51.903 に答える