Hi there, I''m using STI in a new application, and something is terribly wrong with has_many associations. Right, so I have number of subclasses that inherit from one concrete base class. Each sub class can possibly have associations via has_many, or has_one. When I try to access a collection on the sub class( subclass.items), I get ''undefined method ''items'' for <Subclass>''. The strange thing is, accessing these subclasses in script/console works fine, and even works the first time I run the app in some cases. Once I refresh the page I get this error though. I''m totally at a loss here. Any help would be greatly appreciated! Thanks in advance. Mike. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It sounds like you do not have the type field set up properly. You could see this behavior if it was loading the target of the has_many but instantiating the concrete class rather than the subclass. The message may be just reporting the class the association is targeted at. This is just a guess, but fits the description. Michael On Mar 12, 9:34 am, Mike <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi there, > > I''m using STI in a new application, and something is terribly wrong with > has_many associations. > > Right, so I have number of subclasses that inherit from one concrete > base class. Each sub class can possibly have associations via has_many, > or has_one. When I try to access a collection on the sub class( > subclass.items), I get ''undefined method ''items'' for <Subclass>''. The > strange thing is, accessing these subclasses in script/console works > fine, and even works the first time I run the app in some cases. Once I > refresh the page I get this error though. > > I''m totally at a loss here. Any help would be greatly appreciated! > > Thanks in advance. > > Mike. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the input Michael. Well, the thing is, it shows the subclass name in the error message. Undefined method blah for <# subclass name here >. MichaelLatta wrote:> It sounds like you do not have the type field set up properly. You > could see this behavior if it was loading the target of the has_many > but instantiating the concrete class rather than the subclass. The > message may be just reporting the class the association is targeted > at. This is just a guess, but fits the description. > > Michael-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alright, we got it working. Here''s the deal: We had it setup originally with all the subclasses in one file, and we were requiring that in Application.rb. Once I pulled each subclass out, and put it in its own file it took right off. Mike wrote:> > Thanks for the input Michael. > > Well, the thing is, it shows the subclass name in the error message. > Undefined method blah for <# subclass name here >. > > MichaelLatta wrote: >> It sounds like you do not have the type field set up properly. You >> could see this behavior if it was loading the target of the has_many >> but instantiating the concrete class rather than the subclass. The >> message may be just reporting the class the association is targeted >> at. This is just a guess, but fits the description. >> >> Michael-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---