This project is archived and is in readonly mode.

#3809 ✓resolved
Farhad

default scope not changing when class is unloaded and reloaded

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

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.

Comments and changes to this ticket

  • Prem Sichanugrist (sikachu)

    Prem Sichanugrist (sikachu) October 17th, 2010 @ 05:53 PM

    • 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>
    
  • Andrew White

    Andrew White October 19th, 2010 @ 12:49 AM

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

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>

Pages