Fearless Fool
2011-May-05 03:00 UTC
MyModel.descendants returns [] in a view after the first call?
[I posted this initially to the StackOverflow list. There have been no nibbles, so I''m coming here to consult the real experts! :)] I want to display a selection list of MyModel subclasses in a view. It''s not working yet, so for sanity checking, I included this in my view: <%= MyModel.descendants %> The first time I render this page after re-starting the server, it shows the list of descendants (there are six). All subsequent times, it shows up as an empty list []. When invoked from the console, MyModel.descendants reliably returns all six subclasses. FWIW, I have a require statement in my initializers: Dir[Rails.root.join("app/models/my_models/**/*.rb").to_s].each {|f| require f} ... and I''ve verified that they''re getting required. What the @($%& is going on? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Schubert
2011-May-05 04:16 UTC
Re: MyModel.descendants returns [] in a view after the first call?
I am guessing you are in development mode with class caching off :) and that this behavior does not occur in test nor production mode ... Try the code with either test or production if you haven''t already ... I had gone to the place you are in as well, initially my workaround was for my class to read directories and parse .rb files to find sub-classes - LOL and bleyech! If you are seeing that things work just fin in test and production modes, you and I have the same issue - I just posted my solution to this (my fix is not pretty) to the ruby talk mailing list - it works consistently, maybe it will help you. Let me know if you have trouble finding it. Max On 5/4/11, Fearless Fool <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> [I posted this initially to the StackOverflow list. There have been no > nibbles, so I''m coming here to consult the real experts! :)] > > I want to display a selection list of MyModel subclasses in a view. It''s > not working yet, so for sanity checking, I included this in my view: > > <%= MyModel.descendants %> > > The first time I render this page after re-starting the server, it shows > the list of descendants (there are six). All subsequent times, it shows > up as an empty list []. When invoked from the console, > MyModel.descendants reliably returns all six subclasses. > > FWIW, I have a require statement in my initializers: > > Dir[Rails.root.join("app/models/my_models/**/*.rb").to_s].each {|f| > require f} > ... and I''ve verified that they''re getting required. > > What the @($%& is going on? > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 05:22 UTC
Re: MyModel.descendants returns [] in a view after the first call?
@Max: Max Schubert wrote in post #996743:> I am guessing you are in development mode with class caching off :) > and that this behavior does not occur in test nor production mode ... > > Try the code with either test or production if you haven''t already ...Thanks for the tip. Rather than try to get my db migrated over to production mode, I simply edited the line in config/environments/development.rb to read: config.cache_classes = true ... and now it works. BUT: this is a bug, right? - ff P.S.: Max: what''s your handle on ruby talk? I poked around and didn''t see anything obvious -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Schubert
2011-May-05 05:58 UTC
Re: Re: MyModel.descendants returns [] in a view after the first call?
Fearless, On Thu, May 5, 2011 at 1:22 AM, Fearless Fool <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks for the tip. Rather than try to get my db migrated over to > production mode, I simply edited the line in > config/environments/development.rb to read: > > config.cache_classes = true > > ... and now it works. BUT: this is a bug, right?Well, not really a bug - it is ''behavior as designed'' but it is also a ''fail'' for those of us using ''meta-programming as designed LOL'' because meta-programming requires that classes be loaded and that the callbacks associated with Object and Class get called!> P.S.: Max: what''s your handle on ruby talk? I poked around and didn''t > see anything obviousI am using the Google group to respond, so no handle :). Were you able to find the code I used to work around the class caching issue? It is not pretty but it works. - Max -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 06:37 UTC
Re: Re: MyModel.descendants returns [] in a view after the first call?
Max Schubert wrote in post #996758:> Fearless, > Well, not really a bug - it is ''behavior as designed'' but it is also a > ''fail'' for those of us using ''meta-programming as designed LOL'' > because meta-programming requires that classes be loaded and that the > callbacks associated with Object and Class get called!Now I''m intrigued, but that doesn''t explain to me why MyModel.descendants returns a valid list of descendants on the first call but an empty list thereafter. Can you explain why this happens (and why it''s considered behavior as designed)?>> P.S.: I poked around and didn''t see anything obvious > > I am using the Google group to respond, so no handle :). Were you > able to find the code I used to work around the class caching issue?I couldn''t find your code. Without your patch, I can (a) restart the server frequently to avoid the MyModel.descendants bug that I observed, or (b) turn on class caching, which requires me to restart the server between changes. So your patch would be handy. (...I''m still astonished this is how it''s designed to work!!) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-05 06:57 UTC
Re: Re: Re: MyModel.descendants returns [] in a view after the first call?
On 5 May 2011, at 07:37, Fearless Fool <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Max Schubert wrote in post #996758: >> Fearless, >> Well, not really a bug - it is ''behavior as designed'' but it is also a >> ''fail'' for those of us using ''meta-programming as designed LOL'' >> because meta-programming requires that classes be loaded and that the >> callbacks associated with Object and Class get called! > > Now I''m intrigued, but that doesn''t explain to me why > MyModel.descendants returns a valid list of descendants on the first > call but an empty list thereafter. Can you explain why this happens > (and why it''s considered behavior as designed)?descendants returns all loaded subclasses, but when class caching is off, classes are cleared out, so the freshly loaded copy of MyModel has no loaded subclasses. About the best thing you can do is (at the bottom of the base class) use require_dependency to make all the subclasses be loaded when the base class is. By design is perhaps not quite the right word - it''s something people have long been aware, with no obvious solution, but it is obviously not desirable behaviour. Fred> >>> P.S.: I poked around and didn''t see anything obvious >> >> I am using the Google group to respond, so no handle :). Were you >> able to find the code I used to work around the class caching issue? > > I couldn''t find your code. Without your patch, I can (a) restart the > server frequently to avoid the MyModel.descendants bug that I observed, > or (b) turn on class caching, which requires me to restart the server > between changes. So your patch would be handy. > > (...I''m still astonished this is how it''s designed to work!!) > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2011-May-05 07:27 UTC
Re: MyModel.descendants returns [] in a view after the first call?
Use require_dependency to load subclasses. # product.rb class Produtct < AR::Base end require_dependency ''book'' require_dependency ''movie'' # movie.rb class Movie < Product end # book.rb class Book < Product end This way it will work in development and production environment + code reloading will work fine. Another solution is to keep list of class names in database (aka ''dictionary'' table), retrive it and constantize those names. Robert Pankowecki http://robert.pankowecki.pl -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 13:11 UTC
Re: MyModel.descendants returns [] in a view after the first call?
Frederick Cheung wrote in post #996771:> descendants returns all loaded subclasses, but when class caching is > off, classes are cleared out, so the freshly loaded copy of MyModel has > no loaded subclasses.Robert Pankowecki wrote in post #996778:> Use require_dependency to load subclasses.@ Fred: Ah! That makes sense -- now I see why my initialize-time require worked only the first time. @Robert & @Fred: require_dependency it shall be. Thanks all! - ff -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Schubert
2011-May-05 16:53 UTC
Re: Re: MyModel.descendants returns [] in a view after the first call?
Fearless, here is the code pattern I chose. it adds a bunch of custom code to config/environments/development.rb in order to not have to pollute your class heirarchies with ''development only hacks'' code .. I put this below the configure do block in development.rb: i personally prefer this because it is just generating special behavior for the development environment instead of altering behavior on all environments. -= Max if Rails.env.development? # Bootstrap classes being referenced as we use meta-programming that # does not get called in development because class caching # is off. Merely accessing the classes by name triggers the # callbacks that otherwise get called immediately with class # caching on in non-development environments. def bootstrap_x_callbacks X1.nil? X2.nil? X3.nil? X4.nil? X5.nil? X6.nil? end # Run once on startup after Rails environment is all warmed up and # ready to rock. MyApp::Application.configure do config.after_initialize do bootstrap_x_callbacks end end # Schedule it to be called after every reload! ActionDispatch::Callbacks.after do bootstrap_x_callbacks end end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 17:21 UTC
Re: MyModel.descendants returns [] in a view after the first call?
[@Max: our messages just crossed. With all due respect, I think I like my approach better!] Okay. I''m out of the weeds, but I''d like to know if I''ve done it the right way. My code references subclasses of MeteredService several transactions after metered_service.rb gets loaded (e.g. the result of posting a form), at which point the sub-classes had been flushed and there was nothing that forced a reload of metered_service.rb. Ergo, adding require_dependency statements at the bottom of metered_service.rb didn''t help. My next gambit was to overload MeteredService.descendants: class MeteredService ... def self.descendants require_dependency ''metered_services/pge_business'' require_dependency ''metered_services/pge_residential'' require_dependency ''metered_services/sce_residential'' require_dependency ''metered_services/sdge_residential'' require_dependency ''metered_services/socalgas_residential'' require_dependency ''metered_services/srp_residential'' super end end ... but that had the problem that it threw "TypeError (superclass mismatch for class PGEBusiness)" when it got called. Perhaps it''s in the wrong context when it calls require_dependency? I don''t know, and I couldn''t find any documentation that helped. So now I overload MeteredService.descendants, calling ''require'' instead of ''require_dependency''. This seems to work just fine: class MeteredService ... if Rails.env.development? def self.descendants require ''metered_services/pge_business'' require ''metered_services/pge_residential'' require ''metered_services/sce_residential'' require ''metered_services/sdge_residential'' require ''metered_services/socalgas_residential'' require ''metered_services/srp_residential'' super end end end Yes, I will generalize this with a method that reads the contents of the metered_services directory so I don''t have to manually add each one. Let me know if I''m missing anything important. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 17:29 UTC
Re: MyModel.descendants returns [] in a view after the first call?
Fearless Fool wrote in post #996867:> [@Max: our messages just crossed. With all due respect, I think I like > my approach better!] > <snip> > So now I overload MeteredService.descendants, calling ''require'' instead > of ''require_dependency''. This seems to work just fine: > <snip> > Let me know if I''m missing anything important.Aaaaannnnnnd, there just one little problem: it doesn''t work. So my next approach will be some magick with const_defined? -- this time for sure!!! ;) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 17:51 UTC
Re: MyModel.descendants returns [] in a view after the first call?
Fearless Fool wrote in post #996869:> So my next approach will be some magick with const_defined? -- this time > for sure!!! ;)I tried that and now I''m really confused. @Fred: would you expect it to be the case that: class MeteredService < ActiveRecord::Base ; end class PGEBusiness < MeteredService ; end Object.const_defined?("PGEBusiness") => true yet MeteredService.descendants => [] ? That is, the system still knows about the class PGEBusiness, but it doesn''t show up as a descendant of MeteredService? That is, in fact, what I''m seeing. More to the point, how can I tell when I need to reload the subclasses? (And if require_dependencies is the answer, how to I avoid the superclass mismatch error that I''m seeing?) (Feh. Maybe I should just start running in production mode.) TIA. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 18:03 UTC
Re: MyModel.descendants returns [] in a view after the first call?
Resistance is futile. Until someone chimes in with a better idea, I''m adopting Max''s approach (above). - ff -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-05 18:17 UTC
Re: MyModel.descendants returns [] in a view after the first call?
@Max: Fearless Fool wrote in post #996874:> Until someone chimes in with a better idea, I''m adopting Max''s approach > (above).I thought you might want to know: I''ve implemented your approach and I''m STILL seeing the problem: the descendants are defined the first time, but gone thereafter. I put a Rails.logger.debug() call in bootstrap_x_callbacks and I can see it getting called on every reload. But still no joy. Maybe I should take up macramé instead. - ff -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Max Schubert
2011-May-05 21:26 UTC
Re: Re: MyModel.descendants returns [] in a view after the first call?
On Thu, May 5, 2011 at 2:17 PM, Fearless Fool <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @Max: > > Fearless Fool wrote in post #996874: >> Until someone chimes in with a better idea, I''m adopting Max''s approach >> (above). > > I thought you might want to know: I''ve implemented your approach and I''m > STILL seeing the problem: the descendants are defined the first time, > but gone thereafter. I put a Rails.logger.debug() call in > bootstrap_x_callbacks and I can see it getting called on every reload. > But still no joy. > > Maybe I should take up macramé instead.Could you respond with your development.rb file (with names sanitized as needed?) .. what version of Rails are you using? We have been using 3.0.5, just upgrading to 3.0.7 this week .... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-06 07:18 UTC
Re: Re: MyModel.descendants returns [] in a view after the first call?
On 5 May 2011, at 18:51, Fearless Fool <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Fearless Fool wrote in post #996869: >> So my next approach will be some magick with const_defined? -- this time >> for sure!!! ;) > > I tried that and now I''m really confused. @Fred: would you expect it to > be the case that: > > class MeteredService < ActiveRecord::Base ; end > class PGEBusiness < MeteredService ; end > > Object.const_defined?("PGEBusiness") => true > yet > MeteredService.descendants => [] > ? > > That is, the system still knows about the class PGEBusiness, but it > doesn''t show up as a descendant of MeteredService? That is, in fact, > what I''m seeing. >Have you been mixing in calls to require with calls to require_dependency/letting rails load things automatically? That can make a giant mess of things where some classes are reloaded and others aren''t, so for example PGEService could be a subclass of the previous request''s MeteredService Fred> More to the point, how can I tell when I need to reload the subclasses? > (And if require_dependencies is the answer, how to I avoid the > superclass mismatch error that I''m seeing?) > > (Feh. Maybe I should just start running in production mode.) > > TIA. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2011-May-06 09:12 UTC
Re: MyModel.descendants returns [] in a view after the first call?
# product.rb class Produtct < AR::Base end require_dependency ''book'' require_dependency ''movie'' # movie.rb class Movie < Product end # book.rb class Book < Product end This works fine for me in development and production. 1. Development: a) Request comes b) you call Product.dependencies() c) reading product.rb calls require_dependecy and reads movie.rb and book.rb d) Product.dependencies returns => [Movie, Book] e) request is finished. Product, Movie, Book constanst are forgotten. 2. Development: a) Request comes b) you call Movie.something() c) Movie inherits from Product d) autoloading reads product.rb e) reading product.rb calls require_dependecy and reads movie.rb and book.rb f) Movie.something() returns something g) request finished. Product, Movie, Book constanst are forgotten. 3. Production a) Loading rails application leads to require all files in app/**/*.rb for faster forking application. b) movie.rb or product.rb is read first. c) require_dependency behaves like require. d) Everything works still fine. Robert Pankowecki -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-07 04:37 UTC
Re: MyModel.descendants returns [] in a view after the first call?
Max Schubert wrote in post #996908:> Could you respond with your development.rb file (with names sanitized > as needed?) .. what version of Rails are you using? We have been > using 3.0.5, just upgrading to 3.0.7 this week ....Sure. Here it is. Thanks. ===== versions (I''ll move to 3.0.7 when Heroku does): % rake about About your application''s environment Ruby version 1.9.2 (x86_64-darwin10.6.0) RubyGems version 1.3.7 Rack version 1.2 Rails version 3.0.5 Active Record version 3.0.5 Action Pack version 3.0.5 Active Resource version 3.0.5 Action Mailer version 3.0.5 Active Support version 3.0.5 Environment development ===== development.rb (it''s still vanilla): MyApp::Application.configure do config.cache_classes = false config.whiny_nils = true config.consider_all_requests_local = true config.action_view.debug_rjs = true config.action_controller.perform_caching = false config.action_mailer.raise_delivery_errors = false config.action_mailer.default_url_options = { :host => ''localhost:3000'' } config.active_support.deprecation = :log config.action_dispatch.best_standards_support = :builtin end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-07 04:51 UTC
Re: Re: MyModel.descendants returns [] in a view after the first call?
@ Fred, @Robert Pankowecki: Frederick Cheung wrote in post #996972:> Have you been mixing in calls to require with calls to > require_dependency/letting rails load things automatically? That can > make a giant mess of things where some classes are reloaded and others > aren''t, so for example PGEService could be a subclass of the previous > request''s MeteredServiceFred: Your explanation makes sense. I haven''t *intentionally* been mixing calls -- I swore that off back in college after mixing kahlua and tequila. What I *have* tried: [1] adding "require_dependency PGEBusiness'' at the end of its superclass (MeteredService), as suggested by Fred and Robert. It didn''t fix the problem, I THINK because I have a form that passes back "PGEBusiness" several transactions after MeteredService was loaded, by which point the subclasses has been flushed. [2] overloading MeteredService.descendents method as described above: class MeteredService def self.descendants require_dependency ''metered_services/pge_business'' ... super end end ... which resulted in the "TypeError (superclass mismatch for class PGEBusiness)" error I''ve seen. [3] Same as [2], but using ''require'' instead of ''require_dependency''. Um, I think that also got the TypeError message. That was several commits ago -- I can go back and verify that was the problem. So I''m still open to suggestions... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool
2011-May-07 05:13 UTC
Re: MyModel.descendants returns [] in a view after the first call?
@Robert, @Fred, @Max GOT IT! You all had a piece of the puzzle. (Robert: As an aside, you mean "you call Product.descendants()" -- not dependencies() -- but that''s a trifle.) I realized that my reason that the Fred/Robert solution didn''t work was correct: a request passes in the string "PGEBusiness", and the controller code does something like: file: metered_services_controller.rb def create ... class_name = params["metered_service"] # e.g. "PGEBusiness" @metered_service = Object.const_get(class_name).new( ... ) ... end Notice there''s nothing there that forces a load of MeteredService, so the calls to require_dependency() at the bottom of metered_service.rb aren''t invoked. But I can force a load of metered_service.rb by a call to descendants(): def create ... MeteredService.descendants() class_name = params["metered_service"] # e.g. "PGEBusiness" @metered_service = Object.const_get(class_name).new( ... ) ... end And that works. *phew* Thanks for your patience. - ff -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hello everyone! During I probing Rails 3.1.rc1 noticed along the majors many little changes and deprecations. Most of them (changes and deprecations) easy fixable except ONE: If we have: Dog as ActiveRecord class puppy = Dog.new puppy.new_record? => true dumped_puppy = Marshal.load(Marshal.dump(puppy)) dumped_puppy.new_record? => false Why? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.