This project is archived and is in readonly mode.
Default scope and class reloading
-
Andrew White
- Milestone cleared.
- State changed from new to open
- Assigned user set to Andrew White
- Importance changed from to High
Tricky one to track down this. It turns out that AR stores the default scope in a Thread.current variable. This keeps a copy of the class the first time it is loaded and when it's unloaded after each request it becomes stale.
A quick fix is to use the deprecated hash syntax for order:
default_scope :order => 'conversations.updated_at desc' -
Jose
The quick fix doesn't change the behaviour, at least in my case (Rails 3.0.0 + Ruby 1.8.7).
-
Andrew White
Yes, you're right - sorry I thought I'd checked it worked.
Add this block to config/environment/development.rb and it should work even with the new syntax:
config.to_prepare do Thread.current.keys.each{ |k| Thread.current[k] = nil if k.to_s =~ /_scoped_methods$/ } endBasically, it clears out all the cached default scope on each request for all models. This only matters in development so keep it inside development.rb until 3.0.1 gets released.
-
Jose
Brilliant, it works :)
-
Andrew White
Patch for master which adds a callback to ActiveSupport::Dependencies.remove_unloadable_constants! in one commit and then uses that feature to reset the default scope when the class is unloaded in a second commit.
-
Andrew White
Patch for 3-0-stable
-
Andrew White
- Assigned user changed from Andrew White to Xavier Noria
-
Andrew White
Updated patch for master
-
Andrew White
Updated patch for stable
-
Repository
- State changed from open to resolved
(from [aefa11be11a0a6b0c98b776c9e6422b2e0e4b13d]) Reset default scope in Thread.current when class is unloaded [#5497 Default scope and class reloading state:resolved]
Signed-off-by: Xavier Noria fxn@hashref.com
http://github.com/rails/rails/commit/aefa11be11a0a6b0c98b776c9e6422... -
Repository
(from [4e67bf26aa11a59e4f513359208fe3314c5a1ba5]) Reset default scope in Thread.current when class is unloaded [#5497 Default scope and class reloading state:resolved]
Signed-off-by: Xavier Noria fxn@hashref.com
http://github.com/rails/rails/commit/4e67bf26aa11a59e4f513359208fe3...
