hi all, i have a base class ''Message'' and several subclasses ie. ''UserMessage'' < ''Message'' etc. if i want to use one ''create'' form to make a new message but give the option to select the type of message to create, what is the best way to do this? i have thought placing the views in under ''views/message/...'' and then providing a list of subclasses in a dropdown but how do i get a list of subclasses for Message? is there a more elegant way or do i have to provide a controller for each subclass and use a shared layout or something? thanks for your help -felix
Hi, Message.send(:subclasses) would work but may not be the cleanest solution. - Gabriel On 20/10/2005, at 11:45 AM, Felix McCoey wrote:> hi all, > > i have a base class ''Message'' and several subclasses > ie. ''UserMessage'' < ''Message'' > etc. > > if i want to use one ''create'' form to make a new message but give the > option to select the type of message to create, what is the best > way to > do this? > > i have thought placing the views in under ''views/message/...'' and then > providing a list of subclasses in a dropdown but how do i get a > list of > subclasses for Message? > > is there a more elegant way or do i have to provide a controller for > each subclass and use a shared layout or something? > > thanks for your help > > -felix > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Gabriel Gironda wrote:> Hi, > > Message.send(:subclasses) would work but may not be the cleanest solution. > > - Gabriel >that seems to work only if there are already instances of Message''s subclasses. is there are way to get a list of subclasses even if they have not been instantiated? -felix
It should work after the subclasses were parsed & compiled. Rails doesn''t actually require the files until you try to access them. Something like this: Customer => throws Constant not defined rails catches this and checks if there is customer.rb in the load path rails requires customer.rb rails double checks if there is a Customer constant now. rails returns this Customer constant instance and ignores the error. so something like Customer.new can possibly do a whole lot of things between Customer and new. You could put Dir[RAILS_ROOT + ''app/models/*.rb''].each { |f| require f } in your environment.rb to ensure that everything is loaded but this is negatively impact your app startup times... On 10/20/05, Felix McCoey <felix.mccoey-8PZlF8N9LqR+XZJcv9eMoEEOCMrvLtNR@public.gmane.org> wrote:> Gabriel Gironda wrote: > > Hi, > > > > Message.send(:subclasses) would work but may not be the cleanest solution. > > > > - Gabriel > > > > that seems to work only if there are already instances of Message''s > subclasses. is there are way to get a list of subclasses even if they > have not been instantiated? > > -felix > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://jadedpixel.com - modern e-commerce software http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog