This is a similar question to one I posted a couple of days ago. It never got a response, and while I hate to belabor the point, this is really important for the project I''m working on. I''ve got a model that''s a child class of ActiveRecord::Base. Calling any of its methods from my controllers or from the command line (via script/console) works just fine. No problems so far. I take this class and make subclasses from it, storing all the data about these subclasses in one table with a ''type'' column so I can use single-table inheritance to tell them apart. Code looks like this: class PropertyList < ActiveRecord::Base class Property1 < PropertyList Here''s the problem: I can''t access this class directly from my controller, and I can''t call it from the command line. Now, I know that AR isn''t a full-blown object graph persistence framework, but at the very least, I''d expect that when I make a subclass I can instantiate it. I''m fairly new to both Ruby and Rails, coming from a Java/WebObjects background. ''Least surprises'' was one of the things that sold me, and this, to me, is rather surprising. I''m working through the source, trying to figure this out, but if someone with insight would explain this, I''d greatly appreciate it. Perhaps I should try replacing AR with something like Og? Anybody tried/had any luck with this? - Holland
> > I take this class and make subclasses from it, storing all the data > about these subclasses in one table with a ''type'' column so I can use > single-table inheritance to tell them apart. Code looks like this: > > class PropertyList < ActiveRecord::Base > class Property1 < PropertyList > > Here''s the problem: I can''t access this class directly from my > controller, and I can''t call it from the command line. >I have a similar *class* hierarchy (but not *table* hierarchy) and can access sub-classes with no problem. Can you post some more of your model and controller code?
On Aug 10, 2005, at 1:56 PM, Holland wrote:> This is a similar question to one I posted a couple of days ago. It > never got a response, and while I hate to belabor the point, this is > really important for the project I''m working on. > > I''ve got a model that''s a child class of ActiveRecord::Base. Calling > any of its methods from my controllers or from the command line (via > script/console) works just fine. No problems so far. > > I take this class and make subclasses from it, storing all the data > about these subclasses in one table with a ''type'' column so I can use > single-table inheritance to tell them apart. Code looks like this: > > class PropertyList < ActiveRecord::Base > class Property1 < PropertyList > > Here''s the problem: I can''t access this class directly from my > controller, and I can''t call it from the command line. > > Now, I know that AR isn''t a full-blown object graph persistence > framework, but at the very least, I''d expect that when I make a > subclass I can instantiate it. I''m fairly new to both Ruby and Rails, > coming from a Java/WebObjects background. ''Least surprises'' was one of > the things that sold me, and this, to me, is rather surprising. > > I''m working through the source, trying to figure this out, but if > someone with insight would explain this, I''d greatly appreciate it. > > Perhaps I should try replacing AR with something like Og? Anybody > tried/had any luck with this? > > - HollandWhere are you defining your classes that inherit from PropertyList? In order for the loading mechanism to find them with magic you should put each class in its own appropriately named file. Otherwise, if the file hasn''t been loaded yet, the classes will not be found. -Scott
I find this approach to be a pain, I like to put all classes in the same heirarchy in the same file, so I just ''require_dependency'' in the controllers I use them. -Jeff ----- Original Message ----- From: "Scott Barron" <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Wednesday, August 10, 2005 12:19 PM Subject: Re: [Rails] inner workings of AR - would a dev please respond?> > On Aug 10, 2005, at 1:56 PM, Holland wrote: > >> This is a similar question to one I posted a couple of days ago. It never >> got a response, and while I hate to belabor the point, this is really >> important for the project I''m working on. >> >> I''ve got a model that''s a child class of ActiveRecord::Base. Calling any >> of its methods from my controllers or from the command line (via >> script/console) works just fine. No problems so far. >> >> I take this class and make subclasses from it, storing all the data about >> these subclasses in one table with a ''type'' column so I can use >> single-table inheritance to tell them apart. Code looks like this: >> >> class PropertyList < ActiveRecord::Base >> class Property1 < PropertyList >> >> Here''s the problem: I can''t access this class directly from my >> controller, and I can''t call it from the command line. >> >> Now, I know that AR isn''t a full-blown object graph persistence >> framework, but at the very least, I''d expect that when I make a subclass >> I can instantiate it. I''m fairly new to both Ruby and Rails, coming from >> a Java/WebObjects background. ''Least surprises'' was one of the things >> that sold me, and this, to me, is rather surprising. >> >> I''m working through the source, trying to figure this out, but if someone >> with insight would explain this, I''d greatly appreciate it. >> >> Perhaps I should try replacing AR with something like Og? Anybody >> tried/had any luck with this? >> >> - Holland > > Where are you defining your classes that inherit from PropertyList? In > order for the loading mechanism to find them with magic you should put > each class in its own appropriately named file. Otherwise, if the file > hasn''t been loaded yet, the classes will not be found. > > -Scott > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Aug 10, 2005, at 2:21 PM, Jeffrey Moss wrote:> I find this approach to be a pain, I like to put all classes in the > same heirarchy in the same file, so I just ''require_dependency'' in the > controllers I use them. > > -JeffSure, that will also work. -Scott
Thanks for the quick responses - they''ve solved my immediate problem, I think. My top-level class and all of its subclasses live in the same file, so the subclasses weren''t being included automatically. Doing a require_dependency in the appropriate controllers and from script/console seems to fix things for now. Seems a bit odd that this has to be done manually, though. Many thanks for your help... - Holland
> Thanks for the quick responses - they''ve solved my immediate problem, > I think. My top-level class and all of its subclasses live in the > same file, so the subclasses weren''t being included automatically. > Doing a require_dependency in the appropriate controllers and from > script/console seems to fix things for now. Seems a bit odd that this > has to be done manually, though. > > Many thanks for your help...The reason it has to be done manually is you''re not following the rails convention. It has to know where to get the file. If you try to access MyPropertyList, it automaticaly looks for ''my_property_list.rb'' in the rails path. If you had accessed PropertyList first, it would have found ''property_list.rb'' and also loaded its children. Any other framework I''ve worked on [1] makes you put several load statements at the top of your files too. Thankfully, rails helps keep my source files free from that stuff, as long as I follow some basic conventions. Also keep in mind this is a Ruby issue and really has nothing to do with ActiveRecord. I haven''t used og personally, but I don''t see why it wouldn''t work. Just require the og library in environment.rb and it should pick up your og models in app/models the same way. 1 - Compiled languages are a bit different of course. All my types are already compiled in my ASP.Net assemblies, so I don''t have to worry about whether classes are loaded or not. -- rick http://techno-weenie.net
If you put a subclass in it''s parent class''s .rb file, there''s no way ruby would know where to look for the definition. Come to think of it, there''s no way any language or framework would know to look (since it doesn''t know the name of the parent class ''til it sees the subclasses'' definition). Right? It sounds like the problem is that you''re expecting rails to load all files in the models directory at startup. If you really want that behavior, you can do it in environment.rb fairly easily. The default behavior is "lazy loading", I think, because it''s more convenient when you''re developing (you can make changes to model .rb files and your changes are "live" without restarting WEBrick). Tyler On 8/10/05, Holland <holland-0lHqbOJ5DwTR7s880joybQ@public.gmane.org> wrote:> Thanks for the quick responses - they''ve solved my immediate problem, > I think. My top-level class and all of its subclasses live in the > same file, so the subclasses weren''t being included automatically. > Doing a require_dependency in the appropriate controllers and from > script/console seems to fix things for now. Seems a bit odd that this > has to be done manually, though. > > Many thanks for your help... > > - Holland > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Got it, I just wasn''t aware of how far into a file ruby looked when it was loaded. - Holland On Aug 10, 2005, at 4:07 PM, Tyler Kiley wrote:> If you put a subclass in it''s parent class''s .rb file, there''s no way > ruby would know where to look for the definition. Come to think of > it, there''s no way any language or framework would know to look (since > it doesn''t know the name of the parent class ''til it sees the > subclasses'' definition). Right? > > It sounds like the problem is that you''re expecting rails to load all > files in the models directory at startup. If you really want that > behavior, you can do it in environment.rb fairly easily. The default > behavior is "lazy loading", I think, because it''s more convenient when > you''re developing (you can make changes to model .rb files and your > changes are "live" without restarting WEBrick). > > Tyler > > On 8/10/05, Holland <holland-0lHqbOJ5DwTR7s880joybQ@public.gmane.org> wrote: > >> Thanks for the quick responses - they''ve solved my immediate problem, >> I think. My top-level class and all of its subclasses live in the >> same file, so the subclasses weren''t being included automatically. >> Doing a require_dependency in the appropriate controllers and from >> script/console seems to fix things for now. Seems a bit odd that this >> has to be done manually, though. >> >> Many thanks for your help... >> >> - Holland >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Holland Alday Medical Scientist Training Program University of Mississippi Medical Center holland-0lHqbOJ5DwTR7s880joybQ@public.gmane.org