I must be missing something, but I can''t quite seem to get single table inheritance to work. I keep getting NameError: uninitialized constant Admin :(. I have a User table with a ''type'' column and the corresponding models: user.rb: require ''active_record'' class User < ActiveRecord::Base def self.table_name() "user" end end admin.rb: require ''user'' class Admin < User end When I try to access any User (either via a test fixture or db record) I get the NameError. What am I doing wrong? -- Mando
> When I try to access any User (either via a test fixture or db record) > I get the NameError. > > What am I doing wrong?Are you remembering to include require ''admin''? You should probably include that file in user.rb (at the end), so that it''s not possible to require ''user'' without getting admin too. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain
> Are you remembering to include require ''admin''? You should probably > include that file in user.rb (at the end), so that it''s not possible to > require ''user'' without getting admin too.Ah, that did it. I didn''t even think to add the require ''admin'' at the end of user. Thanks a ton, -- Mando
Forgive me if I''ve missed this in the docs, but it is not clear to me how I indicate in my ActiveRecord objects which columns should be associated with which class when I use Single Table Inheritance to associate several classes with a single table. John-Mason Shackelford Software Developer Pearson Educational Measurement 2510 North Dodge St. Iowa City, IA 52245 ph. 319-354-9200x6214 john-mason.shackelford-uJfvbxoDSvxBDgjK7y7TUQ@public.gmane.org http://pearsonedmeasurement.com **************************************************************************** This email may contain confidential material. If you were not an intended recipient, Please notify the sender and delete all copies. We may monitor email to and from our network. ****************************************************************************
On Tue, 7 Dec 2004 15:11:05 -0600, Shackelford, John-Mason <john-mason.shackelford-uJfvbxoDSvxBDgjK7y7TUQ@public.gmane.org> wrote:> Forgive me if I''ve missed this in the docs, but it is not clear to me how I > indicate in my ActiveRecord objects which columns should be associated with > which class when I use Single Table Inheritance to associate several classes > with a single table.In short, you can''t. There was a lengthy discussion about this on this list in the last month or so. Try searching the list archives for "single table inheritance". -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
Forgive me if I''ve missed this in the docs, but it is not clear to me how I indicate in my ActiveRecord objects which columns should be associated with which class when I use Single Table Inheritance to associate several classes with a single table. John-Mason Shackelford Software Developer Pearson Educational Measurement 2510 North Dodge St. Iowa City, IA 52245 ph. 319-354-9200x6214 john-mason.shackelford-uJfvbxoDSvxBDgjK7y7TUQ@public.gmane.org http://pearsonedmeasurement.com
John, Thanks for your response. Is there any ActiveRecord magic to help with a Class Table inheritance model (which I prefer anyway)? Others, Please forgive the accidental duplicate post. John-Mason
> On Tue, 7 Dec 2004 15:11:05 -0600, Shackelford, John-Mason > <john-mason.shackelford-uJfvbxoDSvxBDgjK7y7TUQ@public.gmane.org> wrote: >> Forgive me if I''ve missed this in the docs, but it is not clear to me >> how I >> indicate in my ActiveRecord objects which columns should be associated >> with >> which class when I use Single Table Inheritance to associate several >> classes >> with a single table. > > In short, you can''t. There was a lengthy discussion about this on this > list in the last month or so. Try searching the list archives for > "single table inheritance".Right now all that is configurable is to select the name of the column that contains name of the object you''d like the record to be turned into. Do something like: Person.inheritance_column = ''ruby_type'' Then the adapter will only use STI if it can find a column named ''ruby_type''. Regards, -- Dave Steinberg http://www.geekisp.com/
On Tue, 7 Dec 2004, John-Mason P. Shackelford wrote:> Is there any ActiveRecord magic to help with a Class Table inheritance > model (which I prefer anyway)?At this time, no. There was some discussion in the threads a while back about how to provide this. I''ve looked at it myself, but I find ActiveRecord a bit hard to work with, because it''s structured in a way that makes it hard to use inheritance for "normal" purposes; it seems to be in a world where you''re either ActiveRecord::Base or a direct descendant, and putting stuff in between those two is discouraged. (E.g., you have to start doing hacks like "def self.descends_from_active_record?; true end" in your immediate sublcass of ActiveRecord::Base in order to keep the single-table-inheritance from kicking in.) cjs -- Curt Sampson <cjs-gHs2Wiolu3leoWH0uzbU5w@public.gmane.org> +81 90 7737 2974 http://www.NetBSD.org Make up enjoying your city life...produced by BIC CAMERA
> Is there any ActiveRecord magic to help with a Class Table inheritance > model (which I prefer anyway)?There''s an approach outlined by John Wilger that will make it happen: http://one.textdrive.com/pipermail/rails/2004-November/000439.html To which I accidently replied in private when it should have went to the list:>> DDH, if you''re willing to consider this idea, I''m willing to start >> working on a prototype/patch-set (and anyone else who would want to >> contribute is, obviously, welcome). > > I actually like this idea a lot! And I think it''ll be easy to do with > zero configuration. If a "type" column is available, then SIT is used. > If not, we''ll attempt to use this model. > > If neither is available, we raise a meaningful exception explaining > the two options. > > I too have had cases where the differences where larger than the > similarities, so it''s not like I''m married to SIT. I welcome the > choice.Are you working on this, Wilger? -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain
On Thu, 9 Dec 2004, David Heinemeier Hansson wrote:> > And I think it''ll be easy to do with zero configuration.As you''re about to see, there''s no such thing as "zero configuration."> > If a "type" column is available, then SIT is used. > > If not, we''ll attempt to use this model. > > > > If neither is available, we raise a meaningful exception explaining > > the two options.And so when I pull up a single bit of common code in two of my database classes (what is it here--am I the only ActiveRecord user doing object-oriented programming?), everything blows up? If you don''t make people configure the table inheritance, you then have to let them configure for the case when they''re not doing table inheritance, but only consolidating common behaviour in Ruby classes. Just as an example, think about someone who has five database classes, two of which use some form of table inheritance from a third, and the other two of which are independent. All of them use a primary key consisting of the table name followed by "_id" (which is not the standard ActiveRecord method of naming). So I have this line in five different files: def self.primary_key; table_name + "_id"; end Bing bong! Candidate for "pull up method," right? Try it and everything explodes, until you eventually figure out you also now need def self.descends_from_active_record?; true; end to shut off the single-table inheritance. Presumably you do the same again with false instead of true in every subclass that is supposed to use single-table inheritance from its parent. Is this obvious? Is this the sort of stuff where it just intuitively works? No way. Of course, there''s another way to do this one specific case. You can play around with setting the primary_key_prefix_type class attribute. If you happened to go up and look for it, rather than doing the intuitively obvious thing and making the subclass just DTRT. Sorry, I''m ranting now. But it really annoys me when I try to use basic OO techniques with an external library and that library first of all has superclasses poking around down in the subclasses to see what kind of subclass it is, thereby screwing up the ability of subclass makers to change behaviour, and then goes on to add unnecessary code and complexity to the superclass to compensate for this. That''s so not what OO is about. cjs -- Curt Sampson <cjs-gHs2Wiolu3leoWH0uzbU5w@public.gmane.org> +81 90 7737 2974 http://www.NetBSD.org Make up enjoying your city life...produced by BIC CAMERA
On Thu, 9 Dec 2004 21:16:27 +0900 (JST), Curt Sampson <cjs-gHs2Wiolu3leoWH0uzbU5w@public.gmane.org> wrote:> Is this obvious? Is this the sort of stuff where it just intuitively > works? No way. > > Of course, there''s another way to do this one specific case. You can > play around with setting the primary_key_prefix_type class attribute. If > you happened to go up and look for it, rather than doing the intuitively > obvious thing and making the subclass just DTRT.I agree, I wish the design were such that we could code around unforeseen cases, rather than researching what obscure metastatement might apply to the situation. It''s only my opinion, I suppose, but to me it looks like the current approach has led to a lot of community investment in folk knowledge and "added features" that don''t correct the underlying problem, only fix symptoms.> Sorry, I''m ranting now. But it really annoys me when I try to use basic > OO techniques with an external library and that library first of all > has superclasses poking around down in the subclasses to see what kind > of subclass it is, thereby screwing up the ability of subclass makers > to change behaviour, and then goes on to add unnecessary code and > complexity to the superclass to compensate for this. That''s so not what > OO is about.Echoed. Refactoring AR code is currently a recipe for disaster. I hope someday this will be treated as a bug. -- Ryan Platte
On Thu, 9 Dec 2004 10:40:20 +0100, David Heinemeier Hansson <david-OiTZALl8rpK0mm7Ywyx6yg@public.gmane.org> wrote:> There''s an approach outlined by John Wilger that will make it happen: > Are you working on this, Wilger?I''ve been waiting to see how the discussion plays out before jumping into the code, In the mean-time, I''ve been thoroughly studying the internals of ActiveRecord to gain a better understanding of what''s going on inside it. Is this functionality something you want for the 0.9 release? If so, do you have an expected date? Also, you should probably consider setting up a branch in subversion for this change. It might end up having a large impact on the code, and it would be easier for a lot of people to contribute/test the changes as well as keep up to date with trunk changes that way. Thoughts? -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
> Is this functionality something you want for the 0.9 release? If so, > do you have an expected date?Rails 0.9 is slated for late this week. So if you think this is a big job, it probably won''t be making it in.> Also, you should probably consider setting up a branch in subversion > for this change. It might end up having a large impact on the code, > and it would be easier for a lot of people to contribute/test the > changes as well as keep up to date with trunk changes that way. > Thoughts?Sounds like a good idea. Could you do a preliminary study on how intrusive it will be? I would think that it would stay largely out of the way given the criteria I outlined for choosing between SIT and CIT, but I may be wrong. Looking forward to hearing about your experiences with it! -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain