Topic: class constant from polymorphic relation

Let's say I have a class constant in each model that my polymorphic model can connect to, e.g.

class Cat < ActiveRecord::Base
  NOISE = %w(meow growl)
end

class Dog < ActiveRecord::Base
  NOISE = %w(bark sniff)
end

Now, from the polymorphic model I want to show a select list with the noises from the connected model.

:collection => mypet.pet_type.class.constants.index('NOISE')

is not the answer... Googling 'introspection' isn't helping.

I'm lost. Any ideas?

Thanks!

Re: class constant from polymorphic relation

mypet.pet_type.class::NOISE

...should return the array you want.

Rob Anderton
TheWebFellas

Re: class constant from polymorphic relation

Thanks for your reply. This works great.

I just made a method to call that returns the constant array. About the same except for the loss of elegance and the call/return overhead.

I'll switch it to this method.