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
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 << Player.new() 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:
if active_record.to_s == 'Game'
puts 'Player == compute type: ' + (Player ==
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?