BvdBijl
BvdBijl

Reputation: 607

Why is my scope getting cached?

I have a scope that looks as follows:

scope :top, order('score DESC')

It's used as follows in my controller:

def top
  @users = User.top
  render "list"
end

Now, it seems that this scope is getting cached for no reason at all. If I load the top page, add a user and reload it, the user does not appear in the list.

If I do this however:

def top
  @users = User.order('score DESC')
  render "list"
end

The results don't get cached. What is happening here? I'm using Ruby 2.0.0 and Rails 4.0.0

Upvotes: 4

Views: 1773

Answers (2)

BvdBijl
BvdBijl

Reputation: 607

Found what the problem is! The scope should have been defined as follows:

scope :top, order: 'score DESC'

Upvotes: 1

Christoph Eicke
Christoph Eicke

Reputation: 1144

I think if you use a lambda, then it should not get cached:

scope :top, lambda { order('score DESC') }

But then again, I'm not that familiar with the new Rails 4 scope caching.

Upvotes: 6

Related Questions