This project is archived and is in readonly mode.

#4708 ✓resolved
Rasmus Rønn Nielsen

Query cache broken when using associations with scopes

Reported by Rasmus Rønn Nielsen | May 26th, 2010 @ 04:51 PM

I assume that the following should fire one sql query for each line:

u = User.first
u.posts.older_than 2.seconds
u.posts.older_than 2.hours

For me, it doesn't. The third line does not fire an sql query. Instead, it returns whatever line two returned.

Here's how to repeat this:

1) Create a new Rails app
2) script/rails g model User
3) script/rails g model Post user:references
4) Define this association in user.rb: has_many :posts
5) Define this scope in post.rb: scope :older_than, lambda {|period| where 'created_at < ?', period.ago }
6) Go to the console
7) Run the code written at the top of this ticket while tailing the log file. Only two queries are fired.

I'm using Rails3 beta3 and Ruby 1.9.1 on Mac OSX.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Referenced by