Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

default_scope doesn't work at a late point

#5488

I think there may be a problem with this line:

http://github.com/rails/rails/blob/master/activerecord/lib/active_r...

If scoped_methods invoked at an early point, and you use default_scope to (create a new)/(update existing) default scope later, all current_scoped_methods depending finders (like #all) will still use the old default_scope, because the thread cache is not cleared.

I got this problem when I try to update default scope in my plugin, I need to set Thread.current[key] to nil myself whenever I set a new default scope.

Reported by Jan Xie · August 29th, 2010 @ 04:19 AM

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to open
    • Assigned user set to José Valim
    • Importance changed from to High

    This issue: 4427 must be happening because of this. Although I couldn't reproduce that one in RC 1.

    Assigning to Jose as this looks important.

    August 29th, 2010 @ 12:36 PM

  2. Jan Xie
    Jan Xie

    #4427 Strange behaviour in exception display looks like a different problem.

    I guess add a line in default_scope to clear Thread.current[key] will resolve this problem?

    August 29th, 2010 @ 01:03 PM

  3. José Valim
    José Valim
    • Assigned user changed from José Valim to Rohit Arondekar

    Can you guys provide a patch with tests? Rohit?

    October 11th, 2010 @ 11:19 PM

  4. Jan Xie
    Jan Xie
    • Tag changed from activerecord, default_scope, thread to activerecord, default_scope, patch, thread

    Here's a patch with tests.

    October 12th, 2010 @ 05:45 AM

  5. Rohit Arondekar
    Rohit Arondekar
    • Importance changed from High to Low

    Jan, am I correct in saying that this will have a performance penalty?

    Also I've lowered the priority as I had thought it was related to the other ticket. Apologies.

    October 15th, 2010 @ 10:35 AM

  6. Jan Xie
    Jan Xie

    Do you mean reset_scoped_methods in default_scope will cause the following run more times?

    Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup
    

    default_scope only runs when model classes loading, so I think this is not a big problem?

    Also correctness should be the first goal? ;)

    October 15th, 2010 @ 11:32 AM

  7. Rohit Arondekar
    Rohit Arondekar

    One more doubt, this one is more serious though — the test passes without the fix. I commented out the reset_scoped_methods and ran the suite:

    (ruby-1.9.2@3-dev)(~/projects/rails/activerecord)९ rake test_sqlite3 TEST=test/cases/base_test.rb 2>/dev/null 
    (in /home/rohit/projects/rails/activerecord)
    Using native SQLite3
    Loaded suite /home/rohit/.rvm/gems/ruby-1.9.2-p0@3-dev/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    ............................................................................................................................
    Finished in 1.197417 seconds.
    
    124 tests, 305 assertions, 0 failures, 0 errors, 0 skips
    
    Test run options: --seed 15670
    

    Shouldn't the test fail without the fix?

    October 15th, 2010 @ 11:46 AM

  8. Jan Xie
    Jan Xie

    The real problem is default_scope doesn't merge where clauses correctly. Sorry for my inaccurate description in original ticket.

    October 15th, 2010 @ 11:46 AM

  9. Jan Xie
    Jan Xie

    The new test I added is in relation_scoping_test.rb, try with TEST=test/cases/relation_scoping_test.rb?

    October 15th, 2010 @ 11:48 AM

  10. Rohit Arondekar
    Rohit Arondekar
    • Assigned user changed from Rohit Arondekar to Aaron Patterson

    Sorry! Tried base_test.rb :(

    +1 test fails without the fix and the patch applies cleanly.

    October 15th, 2010 @ 01:33 PM

  11. Repository
  12. Repository
  13. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    November 8th, 2010 @ 08:48 AM