This project is archived and is in readonly mode.
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
-
Neeraj Singh May 26th, 2010 @ 09:22 PM
- Tag changed from 3.0 to 3.0, activerecord
Attached is a failing test. I am using ruby 1.8.7 and am able to reproduce it.
-
Neeraj Singh May 26th, 2010 @ 10:42 PM
It is not a query caching issue. It is named_scope_caching issue. Second time sql is not being generated becuase named_scopes_cache has memoized the input data.
@_named_scopes_cache ||= {} @_named_scopes_cache[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) }
-
Neeraj Singh May 27th, 2010 @ 05:19 PM
- Tag changed from 3.0, activerecord to 3.0, activerecord, patch, scope
Attached is patch along with test.
-
Neeraj Singh June 4th, 2010 @ 01:08 AM
- Assigned user set to Pratik
Assigning this ticket to Pratik so that he could close it. He just committed http://github.com/rails/rails/commit/517f709b51d1d2766d1a783d9f02fa... which fixes this issue.
-
Pratik June 4th, 2010 @ 01:16 AM
- State changed from new to resolved
Sorry, hadn't seen this ticket/patch!
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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 4960 Scopes cached in production mode ticket #4708 has same issue. I fixed that issue by not ca...