Polymorphic associations: finding object types

Hi there

I’m developing an application which makes use of RoR’s polymorphic
associations to associate several components with a single product,
like so:

class Product < ActiveRecord::Base
has_many :line_items, :before_add => :validate_addition
end

class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :resource, :polymorphic => true
end

class Circuit < ActiveRecord::Base
has_one :line_item, :as => :resource
end

class UserAccount < ActiveRecord::Base
has_one :line_item, :as => :resource
end

I’d like to be able to obtain a list of the objects which act as a
resource for use in the interface. I could do this from the database
like so:

mysql> SELECT DISTINCT resource_type FROM line_items;
±--------------+
| resource_type |
±--------------+
| Circuit |
| UserAccount |
±--------------+

which is great if I have at least one line_item acting as a resource,
but if I add a new model (IpRange, say) then my SELECT DISTINCT won’t
pick it up until there is a record in the database.

Does AR provide a handy way of doing this? I can’t see anything
obvious.

Thanks,

Mark

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