Polymorphic associations wrong when used with inherited class?

Consider the following:

class Event
belongs_to :event_object, :polymorphic => true
end

class Asset < ActiveRecord::Base
end

class EventedAsset < Asset
has_one :event, :as => :event_object
end

EventedAsset.find(:first).event generates the following SQL:

SELECT * FROM events WHERE (events.event_object_id = 1 AND
events.event_object_type = ‘Asset’) LIMIT 1

shouldn’t this be:

SELECT * FROM events WHERE (events.event_object_id = 1 AND
events.event_object_type = ‘EventedAsset’) LIMIT 1

I guess i should RTFM.

(here for anyone with the same problem:
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)

Still, IMO, you shouldn’t have to define a crazy method like:

def attachable_type=(sType)
super(sType.to_s.classify.constantize.base_class.to_s)
end

to make STI work with polymorphic associations, it should “just work,”
like everything else does in rails.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs