This project is archived and is in readonly mode.

#5993 ✓stale

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

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

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

Please see the code:


# 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);


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

  # DB settings

class User < ActiveRecord::Base
  has_many :items

  require 'item'

module X

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


class Item < ActiveRecord::Base
  include X

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.

Comments and changes to this ticket

  • kyanagi

    kyanagi November 18th, 2010 @ 09:36 AM

    I could make smaller code which reproduce the problem.

    require 'rubygems'
    require 'active_support/dependencies'
    class X; end
    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.

  • rails

    rails February 19th, 2011 @ 12:00 AM

    • 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.

  • rails

    rails February 19th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

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=""></a>

People watching this ticket