It? unidirectional inheritance crash in Rails 3

Object#is_a? doesn't work in the weirdest way in Rails 3. I have unidirectional table inheritance configured as follows (simplified for brevity):

# resource.rb
class Resource < ActiveRecord::Base
  # blah blah
end

# video.rb
class Video < Resource
  # blah blah
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
  # Do some other stuff
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?

+5
2

, -, Rails. , , . , .

, , , . , .

+2

Video type?

, , , Resource, , , Video.

, Rails! Rails is_a? ActiveSupport::TimeWithZone, Rails - ...

, ?

0

All Articles