Code Monkey home page Code Monkey logo

default_scope_bug's Introduction

default_scope bug

This crashes in Rails 3.0.0 (rc, rc1 and final). The models are the following:

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. This might mean default_scope breaks class reloading.

Solution by Andrew White

Add to development.rb:

config.to_prepare do
  Thread.current.keys.each{ |k| Thread.current[k] = nil if k.to_s =~ /_scoped_methods$/ }
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.