Wrong class in association

I'm using Rails 3.0.0rc and have the following models:

class User
  has_many :readings
  has_many :conversations, :through=>:readings
end

class Reading
  belongs_to :user
  belongs_to :conversation
end

class Conversation
  has_many :readings
  has_many :users, :through=>:readings
end

and this controller code:

class ConversationsController
  def show
    @user = User.find(params[:user_id])
    @conversation = @user.conversations.find(params[:id])
    debugger
  end
end

After requesting conversations#show, go to debug console and:

User.class.object_id

=> 116350

@conversation.users.first.class.object_id

=> 33660870

@conversation.users.first.instance_of? User

=> true # Right

Then leave debugging and request the page again:

User.class.object_id

=> 116350

@conversation.users.first.class.object_id

=> 36497930

@conversation.users.first.instance_of? User

=> true # Right

And then, request the page again:

User.class.object_id

=> 116350

@conversation.users.first.class.object_id

=> 36497930

@conversation.users.first.class.to_s == User.to_s

=> true

@conversation.users.first.instance_of? User

=> false # Wrong!

This last evaluation is wrong, and breaks equality checks and
expressions such as @conversation.users.include?(some_user)

If I disable class caching, the problem disappears (but that's not
ideal for development). Maybe I'm doing something that breaks
@reflection in association_proxy.rb? Any ideas?

This last evaluation is wrong, and breaks equality checks and
expressions such as @conversation.users.include?(some_user)

If I disable class caching, the problem disappears (but that's not
ideal for development). Maybe I'm doing something that breaks
@reflection in association_proxy.rb? Any ideas?

It looks as though you're keeping a reference to the conversation object between requests. The association class is cached in the association's reflection information and if you don't re-create the object every request you'll get an old copy of the User class. This only affects development mode where the classes are reloaded on every request.

You can reproduce the effect in the console:

c = Conversation.first

=> #<Conversation id: 1, created_at: "2010-08-30 05:07:14", updated_at: "2010-08-30 05:07:14">

c.users.first.instance_of?(User)

=> true

reload!

Reloading...
=> true

c.users.first.instance_of?(User)

=> false

c = Conversation.first

=> #<Conversation id: 1, created_at: "2010-08-30 05:07:14", updated_at: "2010-08-30 05:07:14">

c.users.first.instance_of?(User)

=> true

Andrew