# Wrong class being returned in association

**URL:** <https://www.ruby-forum.com/t/wrong-class-being-returned-in-association/194057>\
**Category:** Rails\
**Created:** [August 29, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/wrong-class-being-returned-in-association/194057 "2010-08-29T12:39:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [August 29, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/wrong-class-being-returned-in-association/194057/1 "2010-08-29T12:39:17Z")

</div>

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 enable 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?

---

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [August 29, 2010, 2:19pm UTC](https://www.ruby-forum.com/t/wrong-class-being-returned-in-association/194057/2 "2010-08-29T14:19:04Z")

</div>

On Aug 29, 11:39 am, José Ignacio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This last evaluation is wrong, and breaks equality checks and  
> expressions such as @conversation.users.include?(some\_user)
> 
> If I enable 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?

Sounds like you might be breaking class reloading somehow. It’s  
possible to do this in such a way that you have both new and old  
versions of a class, which are then not equal. In the past I’ve found  
explicit require statements (rather than require\_dependency) could do  
that (because require doesn’t (or at least didn’t) invoke the  
dependency stuff at all). Models in plugins can also do this, because  
typically plugins aren’t reloaded, so if you have an association  
between a plugin model (that doesn’t get reloaded) and an application  
model (which does) then weird stuff happens.

Fred

---

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [August 29, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/wrong-class-being-returned-in-association/194057/3 "2010-08-29T14:24:11Z")

</div>

That makes a lot of sense. I’m using authlogic and acts\_as\_follower in  
the user model. Turning them off to check which one is producing the  
issue is hard, but I’ll have to try. Is there a way to force plugin  
classes to reload? Or, what could be the ‘best practice’ that is not  
being followed in these plugins?

---

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [August 29, 2010, 2:30pm UTC](https://www.ruby-forum.com/t/wrong-class-being-returned-in-association/194057/4 "2010-08-29T14:30:11Z")

</div>

On Aug 29, 1:24 pm, José Ignacio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That makes a lot of sense. I’m using authlogic and acts\_as\_follower in  
> the user model. Turning them off to check which one is producing the  
> issue is hard, but I’ll have to try. Is there a way to force plugin  
> classes to reload? Or, what could be the ‘best practice’ that is not  
> being followed in these plugins?

In 2.3 you can remove stuff from ActiveSupport::load\_once\_paths -  
remove the path to the plugin’s lib dir and you should be fine (I  
think by the time initializers run plugins are all setup so that’s  
probably a good a place as any to do that)

Fred

---

<div class="post-metadata">

**Author:** ![ji123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@ji123](https://www.ruby-forum.com/u/ji123)\
**Post date:** [August 29, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/wrong-class-being-returned-in-association/194057/5 "2010-08-29T16:44:26Z")

</div>

After some research it seems that’s not the cause of the problem. I  
created a blank project, removed all plugins and the error persisted:

> User.last.conversations.find(Conversation.last.id).users.last.instance\_of?(User)  
> =\> true  
> reload!  
> Reloading…  
> =\> true  
> User.last.conversations.find(Conversation.last.id).users.last.instance\_of?(User)  
> =\> false

I removed this line I had in the Conversation model:

default\_scope :order =\> ‘updated\_at desc’

Now the models are exactly as shown in my first post, and now there’s no  
class bug. Of course, I have been testing with only one user, one  
conversation and one reading (otherwise the order in the default scope  
would make a big change in the query).
