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?
--
Posted via http://www.ruby-forum.com/.
> 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).