Gareth Adams
2008-Feb-21 11:34 UTC
Object.subclasses_of(MyModel) not working in development environment
I''m trying to use the subclasses_of method but I''m getting strange results. The first request after I restart the server, everything works fine. Subsequent requests return an empty array. Can anyone shed any light on why this happens or what I can do about it? Thanks, Gareth --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-21 11:53 UTC
Re: Object.subclasses_of(MyModel) not working in development environment
On 21 Feb 2008, at 11:34, Gareth Adams wrote:> > I''m trying to use the subclasses_of method but I''m getting strange > results. > > The first request after I restart the server, everything works fine. > Subsequent > requests return an empty array. > > Can anyone shed any light on why this happens or what I can do about > it?This is probably to do with rails'' magic unloading of your classes after the request has run. Be aware that subclasses_of might not give you all your subclasses, because if the file hasn''t been loaded yet then subclasses_of won''t know about it. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Gareth Adams
2008-Feb-21 12:19 UTC
Re: Object.subclasses_of(MyModel) not working in development environment
Frederick Cheung <frederick.cheung@...> writes:> On 21 Feb 2008, at 11:34, Gareth Adams wrote: > > > Can anyone shed any light on why this happens or what I can do about > > it? > > This is probably to do with rails'' magic unloading of your classes > after the request has run. Be aware that subclasses_of might not give > you all your subclasses, because if the file hasn''t been loaded yet > then subclasses_of won''t know about it.Thanks, Fred, I''m aware that not all models are loaded by default and I have a Dir[...].each {|f| require f} to make sure the relevant subclasses *are* loaded when I need to access them. I did know that Rails had some magic to let you see your updated models without having to restart your server every request, but I''m still confused why Ruby doesn''t see these classes in subsequent requests. If they''re unloaded at the end of the request then surely they get *re*loaded the next time I need them - that''s the point, isn''t it? Gareth --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-21 12:54 UTC
Re: Object.subclasses_of(MyModel) not working in development environment
On 21 Feb 2008, at 12:19, Gareth Adams wrote:> > Frederick Cheung <frederick.cheung@...> writes: > >> On 21 Feb 2008, at 11:34, Gareth Adams wrote: >> >>> Can anyone shed any light on why this happens or what I can do about >>> it? >> >> This is probably to do with rails'' magic unloading of your classes >> after the request has run. Be aware that subclasses_of might not give >> you all your subclasses, because if the file hasn''t been loaded yet >> then subclasses_of won''t know about it. > > Thanks, Fred, > > I''m aware that not all models are loaded by default and I have a > Dir[...].each > {|f| require f} to make sure the relevant subclasses *are* loaded > when I need to > access them. > > I did know that Rails had some magic to let you see your updated > models without > having to restart your server every request, but I''m still confused > why Ruby > doesn''t see these classes in subsequent requests. If they''re > unloaded at the end > of the request then surely they get *re*loaded the next time I need > them - > that''s the point, isn''t it?One thing that does happen is that when rails unloads stuff, it essentially empties out existing classes. If you''re somehow hanging onto an old reference to MyModel and call subclasses_of on that then it''s possible that you''d get back an empty array Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---