Title: Issues with n+1 queries in Rails Active Record
Tags: ruby-on-rails;activerecord
Question: I'm looking to assign past, current and future requests of a user to variables. He is my code:  

```@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 &gt;= ?", Date.today).last(params[:offset].present? ? params[:offset] : "5" )

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

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

The issue is that I get hundreds of logs like below when doing this:

```
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1
  LIMIT $2  [["id", 39], ["LIMIT", 1]]
```

I'm not sure why this is the case and how I can solve this?

Comment: this is active record cache, most likely current_user method. It doesnt hit db, so doesnt hurt performance

Comment: did you define current_user yourself? In your context, I guess current user has id 39

Comment: weird because you get the user out of email, and the cached query is by id

Comment: Oh ok so when it's written CACHE next to it, it can't hurt performance? It's still a mystery to me why I have hundreds of line of these for just a few queries. @apneadiving

Comment: I'm using this for an API, the current_user is the @user above.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:02/19/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ruby-on-rails><activerecord>', 'Title': 'Issues with n+1 queries in Rails Active Record', 'ViewCount': 40, 'id': 48861869, 'user_AboutMe': '', 'user_AccountId': 7970965.0, 'user_DisplayName': 'Hugo', 'user_Reputation': 1855.0, 'user_Views': 237.0, 'user_id': 6015670.0}}