Forum: Ruby on Rails Player != Player?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
10741db2c769aebcfb83033e0b64bf01?d=identicon&s=25 cainlevy (Guest)
on 2006-12-31 00:38
(Received via mailing list)
I'm developing an hobby application designed to provide some utilities
for a favorite boardgame of mine. I can't say exactly what problem I'm
having, but I can describe two symptoms that I've discovered have the
same root:

1.) When loading a page for the *second* time since the server started,
@player.technologies turns up method_missing (Player.has_many

2.) When loading a page for the *second* time since the server started,
@game.players << raises an AssocationTypeMismatch
complaining that "Player expected, got Player". Huh?

While investigating the first I discovered that on the *second* page
load since the server started, @player.reflect_on_all_associations.size
== 0. But on the first page load, there were 6! So somehow it "lost" my
associations. I did a sanity check. Yep, on the second page load
Player.reflections.size == 6 but @player.class.reflections == 0 ... yet
@player.class.to_s == 'Player'. Wow. So confused.

Well further digging revealed that on the second page load
@player.class != Player even though @player.class.to_s == Player.to_s.
Huh? That's the same problem as issue #2.

Ok, so now at least I know the problems have the same root cause. I did
a lot more tracing through ActiveRecord. Stayed up a few hours. Whipped
myself with a wet noodle. Ended up putting some debugging messages into
ActiveRecord::Reflection::AssocationReflection.klass. Whenever that
method gets called, if self == Game I do some sanity checks to see if
@klass == Player.

    class AssociationReflection < MacroReflection #:nodoc:
      def klass
        if active_record.to_s == 'Game'
          puts 'Player == compute type: ' + (Player ==
active_record.send(:compute_type, class_name)).to_s
          puts 'Player == @klass: ' + (Player == @klass).to_s if @klass
        @klass ||= active_record.send(:compute_type, class_name)

On the first page load, both sanity checks return true. On the second
page load, the first returns true and the second returns false. That
is, on the first page load @klass == Player, but on the second page
load @klass != Player (even though @klass.to_s == 'Player' all the way

What's up? Anyone? Do I need a bigger noodle?
This topic is locked and can not be replied to.