0

我们有一个动作,它返回一个用户电子邮件的动态列表,这些电子邮件是每月通讯到期的。该操作采用一个可选mark_as_sent参数,如果存在,它将更新用户记录以显示他们今天收到了一封电子邮件。

但是即使更新操作是在用户被选中之后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
4

1 回答 1

1

Rails 延迟加载来自第一个查询的数据,并立即执行第二个查询。尝试强制第一个查询:

@users = User.where("monthly_email_sent < '#{last_month}' AND on_mailing_list = 't'").select("email","id").to_a
于 2014-02-11T05:15:09.307 回答