Object#is_a? doesn't work in the weirdest way in Rails 3. I have unidirectional table inheritance configured as follows (simplified for brevity):
class Resource < ActiveRecord::Base
end
class Video < Resource
end
In my controller, I have this:
def create
@resource = Resource.find params[:resource_id]
logger.info '@resource class: ' + @resource.class.name
logger.info '@resource superclass: ' + @resource.class.superclass.name
logger.info '@resource is_a?(Video): ' + @resource.is_a?(Video).inspect
logger.info '@resource is_a?(Resource): ' + @resource.is_a?(Resource).inspect
logger.info '@resource is_a?(ActiveRecord::Base): ' + @resource.is_a (ActiveRecord::Base).inspect
end
An action call #creategenerates the following results:
@resource class: Video
@resource superclass: Resource
@resource is_a?(Video): true
@resource is_a?(Resource): false
@resource is_a?(ActiveRecord::Base): true
Please note that the instance Video a ActiveRecord::Base, but not a Resource. This is not just an academic problem. The Framework code called from the action uses is_a?to check for type mismatch, and it rises when it considers that Videoit is not Resource.
However, the Rails console is_a?(Resource)returns true.
What in the world can happen here?