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 not changing when class is unloaded and reloaded

#3809

Rails 2.3.4

It seems that the default_scope doesn't change when reloading an object.

To recreate:

class Foo < ActiveRecord::Base
 puts "Loading Foo"
 default_scope :conditions => "id < 10"
end

If in the console I type:

> Foo.count

I see the following appearing in the output log stream:

DEBUG: Jan 28 17:57:12: SQL (0.3ms) SELECT count(*) AS count_all FROM foos WHERE (id < 10)

Now, change the default scope such that the conditions are:

class Foo < ActiveRecord::Base
 puts "Loading Foo"
 default_scope :conditions => "id > 5"
end

In console do the following:

>> reload!
Reloading...
=> true
>> Foo.count
Loading Foo

We see in the output log:

DEBUG: Jan 28 18:02:24: SQL (0.2ms) SELECT count(*) AS count_all FROM foos WHERE (id < 10)

but if I exit console and run Foo.count again I get the correct response:

DEBUG: Jan 28 18:03:38: SQL (0.2ms) SELECT count(*) AS count_all FROM foos WHERE (id > 5)

NOTE: the system is rails 2.3.4 but we've changed the output log to match what we have in our other systems running 1.2.6 for compatibility with our log parsers.

Reported by Farhad · January 29th, 2010 @ 02:12 AM

State: resolved
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • State changed from new to resolved
    • Importance changed from to Low

    Hi,

    On the current master, it seems like this issue has been fixed.

    # Foo.rb
    class Foo < ActiveRecord::Base
      puts "Loading Foo"
      default_scope where("id < 10")
    end
    
    # Rails console
    Loading development environment (Rails 3.1.0.beta)
    irb(main):001:0> Foo.count
    Loading Foo
      SQL (0.1ms)  SELECT COUNT(*) FROM "foos" WHERE (id < 10)
    => 0
    

    Change the default scope

    # Foo.rb
    class Foo < ActiveRecord::Base
     puts "Loading Foo"
     default_scope where("id > 5")
    end
    
    # Rails console
    irb(main):002:0> reload!
    Reloading...
    => true
    irb(main):003:0> Foo.count
    Loading Foo
      SQL (0.2ms)  SELECT COUNT(*) FROM "foos" WHERE (id > 5)
    => 0
    irb(main):004:0>
    

    October 17th, 2010 @ 05:53 PM

  2. Andrew White
    Andrew White

    Yes, it was fixed by the commits on #5497 Default scope and class reloading - though looking at this it needs backporting to 2-3-stable as well.

    October 19th, 2010 @ 12:49 AM