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.

A class of associated object is not equal to the original one.

#5993

I found some strange behavior with 3.0.3. (ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux])

Please see the code:

database:

# create table users (id integer auto_increment, primary key (id));
# create table items (id integer auto_increment, user_id integer, primary key (id));
# insert into users (id) values (1);
# insert into items (user_id) values (1);

a.rb:

require 'rubygems'
require 'active_support/dependencies'
require 'active_record'

ActiveRecord::Base.establish_connection(
  # DB settings
)

class User < ActiveRecord::Base
  has_many :items
end

begin
  require 'item'
rescue
end

module X
end

require 'item'

u = User.first
item = u.items.first
p item
p item.class
p item.class.object_id, Item.object_id # should be same but differ
p item.instance_of?(Item) # should be true but becomes false

item.rb:

class Item < ActiveRecord::Base
  include X
end

I put two files a.rb and item.rb.

When I executed a.rb (ruby a.rb), I got different object_id between item.class and Item, and item.instance_of?(Item) became false.

I could not fix correctly. But I found that if the line in ActiveSupport::Dependencies#remove_contant (call of constantize)

constantize(const).before_remove_const if constantize(const).respond_to?(:before_remove_const)

is commented out, the problem disappears.

Reported by kyanagi · November 17th, 2010 @ 03:33 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. kyanagi
    kyanagi

    I could make smaller code which reproduce the problem.

    require 'rubygems'
    require 'active_support/dependencies'
    
    class X; end
    ActiveSupport::Dependencies.remove_constant('X')
    class X; end
    
    p X.object_id == ActiveSupport::Dependencies.constantize('X').object_id
    

    ActiveSupport::Dependencies.constantize caches its result in ActiveSupport::Dependencies::Reference.
    remove_constant removes a constant but does not clear cache.

    I think remove_constant should clear the cache.

    November 18th, 2010 @ 09:36 AM

  2. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 19th, 2011 @ 12:00 AM

  3. rails
    rails
    • State changed from open to stale

    February 19th, 2011 @ 12:00 AM