Rails Active Record中n + 1个查询的问题

时间:2018-02-19 08:13:02

标签: ruby-on-rails activerecord

我希望将用户的过去,当前和未来请求分配给变量。他是我的代码:

@user = User.find_by(email: params[:email])

req = Request.where("user_id = ? OR item_id = ?",  @user.id, @user.item.first.id)

@next = req.where("start_datetime >= ?", Date.today).last(params[:offset].present? ? params[:offset] : "5" )

@current = req.where("start_datetime > ?", Date.today.beginning_of_day).where("end_datetime > ?", (Time.now - 30.minutes)).where(status: 'approved').order(:start_datetime).first || req.where("start_datetime > ?", Time.now).where(status: 'approved').order(:start_datetime).first

@past = req.where("start_datetime < ?", Date.today).last(5)

问题是我在执行此操作时会收到如下所示的数百个日志:

  

CACHE(0.0ms)SELECT“users”。* FROM“users”WHERE“users”。“id”= $ 1   LIMIT $ 2 [[“id”,39],[“LIMIT”,1]]

我不确定为什么会这样,我怎么解决这个问题?

0 个答案:

没有答案