0
我們有一個操作,它返回一份每月新聞快遞到期的用戶電子郵件動態列表。該操作採用可選的mark_as_sent
參數,如果存在該參數,則會更新用戶記錄以顯示他們今天發送了電子郵件。Rails控制器操作中意外的sql操作順序
但即使在之後發生更新操作用戶已被選中,但當mark_as_sent
爲真時,該操作總是返回空列表。
這兩個操作不是同一個事務的一部分,所以我不明白爲什麼第一個操作似乎在第二個操作之前生效?
def monthly_email_list
last_month = DateTime.current-1.month
# Grab all users who havent received a monthly follow up email in a month or more:
@users = User.where("monthly_email_sent < '#{last_month}' AND on_mailing_list = 't'").select("email","id")
if params[:mark_as_sent]
count = User.where("monthly_email_sent < '#{last_month}'").select("email","id").update_all(:monthly_email_sent => DateTime.current)
end
end
就是這樣。我最終使用'User.where(...).all',因爲'.all'可以防止延遲加載,如[here](http://m.onkey.org/active-record-query-interface)所述。似乎與'.to_a'相同 – Yarin