This project is archived and is in readonly mode.

#5497 ✓resolved
Jose

Default scope and class reloading

Reported by Jose | August 30th, 2010 @ 01:59 PM | in 3.0.2

This crashes in Rails 3.0.0 (rc, rc1 and final). Clone this source to get a clean project that reproduces the bug: http://github.com/josei/default_scope_bug

Bascially, the project considers the following models:

class Conversation < ActiveRecord::Base
  has_many :readings
  has_many :users, :through => :readings
  default_scope order('conversations.updated_at desc')
end
class Reading < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
end
class User < ActiveRecord::Base
  has_many :readings
  has_many :conversations, :through => :readings
end

Perform these steps to reproduce the bug:

$ rake db:migrate

$ rails c

irb(main):001:0> User.create :name=>'Joe'
=> #<User id: 1, name: "Joe", created_at: "2010-08-30 12:25:41", updated_at: "2010-08-30 12:25:41">
irb(main):002:0> User.first.conversations.create :title=>'New conversation'
=> #<Conversation id: 1, title: "New conversation", created_at: "2010-08-30 12:25:55", updated_at: "2010-08-30 12:25:55">
irb(main):003:0> User.first.conversations.find(Conversation.first.id).users.first.is_a?(User)
=> true
irb(main):004:0> reload!
Reloading...
=> true
irb(main):005:0> User.first.conversations.find(Conversation.first.id).users.first.is_a?(User)
=> false

You can also check object_ids for class before reloading and after; it looks like there's a problem in class versioning. The line that causes the problem is default_scope at conversation.rb. You can try commenting out that line and repeating the last steps to check it now works:

$ rails c

irb(main):001:0> User.first.conversations.find(Conversation.first.id).users.first.is_a?(User)
=> true
irb(main):002:0> reload!
Reloading...
=> true
irb(main):003:0> User.first.conversations.find(Conversation.first.id).users.first.is_a?(User)
=> true

This is not a problem with class caching disabled, but is annoying in development mode. It seems that default_scope breaks class reloading and something related with @reflection in association_proxy.rb.

Comments and changes to this ticket

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>

Referenced by

Pages