I''m currently coding a system which must store multiple contact methods for a user (phone, email, postal address etc). I''m planning a fairly straightforward inheritance hierachy for these, where each different method inherits from something like a ContactMethod class. The only mention of ActiveRecord support for inheritance I can find in the Wiki and the Agile Web Dev book is STI. What''s not clear is whether STI is the *only* way that this will work. If I define my classes to have inheritance relationships and then create a separate table for each subclass will it not work? Many thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/ec1429ab/attachment.html
On Tue, Jan 17, 2006 at 08:42:51AM -0000, Jonathan Telfer wrote:> I''m currently coding a system which must store multiple contact methods for > a user (phone, email, postal address etc). I''m planning a fairly > straightforward inheritance hierachy for these, where each different method > inherits from something like a ContactMethod class. > > The only mention of ActiveRecord support for inheritance I can find in the > Wiki and the Agile Web Dev book is STI. What''s not clear is whether STI is > the *only* way that this will work. If I define my classes to have > inheritance relationships and then create a separate table for each subclass > will it not work?I tried this over the weekend, thinking that it wouldn''t be huge issue. What I had was a fairly simple hierarchy, like this: User / \ / \ Admin Customer Both Admins and Customers could log in, so the User class has all the common stuff (login, password changing, etc), but there''s no table for User. It''s this last part that caused the problem for me. The moment I tried to run something on the User class (even if the method had been overridden by me), the User class attempted to access the table users and it all fell apart. I think that if you had a table behind your base class, it *might* work, but that seems pretty pointless. The way I sorted the problem was to create a User class, containing all of the class methods, and a UserMixin module, for the benefit of the "subclasses". It''s a pity that it needs to be done this way, and I fully agree that it''s not the most OO way of doing things, but if it becomes a common, standard idiom, it won''t be a major problem. Of course, if we really want to support inheritance, then we want to throw out this ORM business and go to a real object database... - Matt
Rodrigo Alvarez Fernández
2006-Jan-17 10:47 UTC
[Rails] Is STI the only way to do inheritance?
On 1/17/06, Jonathan Telfer <jtelfer@ntlworld.com> wrote:> > I''m currently coding a system which must store multiple contact methods > for a user (phone, email, postal address etc). I''m planning a fairly > straightforward inheritance hierachy for these, where each different method > inherits from something like a ContactMethod class. > > The only mention of ActiveRecord support for inheritance I can find in the > Wiki and the Agile Web Dev book is STI. What''s not clear is whether STI is > the *only* way that this will work. If I define my classes to have > inheritance relationships and then create a separate table for each subclass > will it not work? > >Rails only support STI, but there are two more methods/patterns: http://wiki.rubyonrails.com/rails/pages/SingleTableInheritance You can read the Martin Fowler''s explanations listed there. Here http://dev.rubyonrails.org/ticket/600 you can find a patch for a Class Table inheritance implementation. If yoy try it, please share your experiencie with it here :]. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/5053a118/attachment.html
Matt, I did the same thing, where you are trying to do STI on 2 out of your 3 Models, and I posted here, under ''subclassing part of STI'', but got no response. Since my example only needed one deviation from the normal Model (Telecommunications Room is a subclass of Room), I just added another column for boolean to delineate the type. I''m going to try the links posted, I hope there is some other workaround to adding another column, but in the long-run, once you create the helper to only return the ''subclasses'' based on a boolean type, you''re going to work with it like any other Model. - Nic. On 1/17/06, Matthew Palmer <mpalmer@hezmatt.org> wrote:> On Tue, Jan 17, 2006 at 08:42:51AM -0000, Jonathan Telfer wrote: > > I''m currently coding a system which must store multiple contact methods for > > a user (phone, email, postal address etc). I''m planning a fairly > > straightforward inheritance hierachy for these, where each different method > > inherits from something like a ContactMethod class. > > > > The only mention of ActiveRecord support for inheritance I can find in the > > Wiki and the Agile Web Dev book is STI. What''s not clear is whether STI is > > the *only* way that this will work. If I define my classes to have > > inheritance relationships and then create a separate table for each subclass > > will it not work? > > I tried this over the weekend, thinking that it wouldn''t be huge issue. > What I had was a fairly simple hierarchy, like this: > > User > / \ > / \ > Admin Customer > > Both Admins and Customers could log in, so the User class has all the common > stuff (login, password changing, etc), but there''s no table for User. > > It''s this last part that caused the problem for me. The moment I tried to > run something on the User class (even if the method had been overridden by > me), the User class attempted to access the table users and it all fell > apart. > > I think that if you had a table behind your base class, it *might* work, but > that seems pretty pointless. > > The way I sorted the problem was to create a User class, containing all of > the class methods, and a UserMixin module, for the benefit of the > "subclasses". It''s a pity that it needs to be done this way, and I fully > agree that it''s not the most OO way of doing things, but if it becomes a > common, standard idiom, it won''t be a major problem. > > Of course, if we really want to support inheritance, then we want to throw > out this ORM business and go to a real object database... > > - Matt > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- - Nic
On Jan 17, 2006, at 3:37 AM, rails-request@lists.rubyonrails.org wrote:> On Tue, Jan 17, 2006 at 08:42:51AM -0000, Jonathan Telfer wrote: >> I''m currently coding a system which must store multiple contact >> methods for >> a user (phone, email, postal address etc). I''m planning a fairly >> straightforward inheritance hierachy for these, where each >> different method >> inherits from something like a ContactMethod class. >> >> The only mention of ActiveRecord support for inheritance I can >> find in the >> Wiki and the Agile Web Dev book is STI. What''s not clear is >> whether STI is >> the *only* way that this will work. If I define my classes to have >> inheritance relationships and then create a separate table for >> each subclass >> will it not work? > > I tried this over the weekend, thinking that it wouldn''t be huge > issue. > What I had was a fairly simple hierarchy, like this: > > User > / \ > / \ > Admin Customer > > Both Admins and Customers could log in, so the User class has all > the common > stuff (login, password changing, etc), but there''s no table for User. > > It''s this last part that caused the problem for me. The moment I > tried to > run something on the User class (even if the method had been > overridden by > me), the User class attempted to access the table users and it all > fell > apart. > > I think that if you had a table behind your base class, it *might* > work, but > that seems pretty pointless. > > The way I sorted the problem was to create a User class, containing > all of > the class methods, and a UserMixin module, for the benefit of the > "subclasses". It''s a pity that it needs to be done this way, and I > fully > agree that it''s not the most OO way of doing things, but if it > becomes a > common, standard idiom, it won''t be a major problem. > > Of course, if we really want to support inheritance, then we want > to throw > out this ORM business and go to a real object database...You have to add two methods to your abstract classes to make inheritance work the way it should: http://wiki.rubyonrails.org/rails/pages/HowtoMakeAbstractModel -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com
Just glancing at it, would this AbstractModel allow for a generic Node object, where you can create more specific things below it? I''m looking for a similiar application that would let me hop from Node to Node, and to jump into the models below it and grab the data dynamically. - Nic. On 1/17/06, Eric Hodel <drbrain@segment7.net> wrote:> On Jan 17, 2006, at 3:37 AM, rails-request@lists.rubyonrails.org wrote: > > On Tue, Jan 17, 2006 at 08:42:51AM -0000, Jonathan Telfer wrote: > >> I''m currently coding a system which must store multiple contact > >> methods for > >> a user (phone, email, postal address etc). I''m planning a fairly > >> straightforward inheritance hierachy for these, where each > >> different method > >> inherits from something like a ContactMethod class. > >> > >> The only mention of ActiveRecord support for inheritance I can > >> find in the > >> Wiki and the Agile Web Dev book is STI. What''s not clear is > >> whether STI is > >> the *only* way that this will work. If I define my classes to have > >> inheritance relationships and then create a separate table for > >> each subclass > >> will it not work? > > > > I tried this over the weekend, thinking that it wouldn''t be huge > > issue. > > What I had was a fairly simple hierarchy, like this: > > > > User > > / \ > > / \ > > Admin Customer > > > > Both Admins and Customers could log in, so the User class has all > > the common > > stuff (login, password changing, etc), but there''s no table for User. > > > > It''s this last part that caused the problem for me. The moment I > > tried to > > run something on the User class (even if the method had been > > overridden by > > me), the User class attempted to access the table users and it all > > fell > > apart. > > > > I think that if you had a table behind your base class, it *might* > > work, but > > that seems pretty pointless. > > > > The way I sorted the problem was to create a User class, containing > > all of > > the class methods, and a UserMixin module, for the benefit of the > > "subclasses". It''s a pity that it needs to be done this way, and I > > fully > > agree that it''s not the most OO way of doing things, but if it > > becomes a > > common, standard idiom, it won''t be a major problem. > > > > Of course, if we really want to support inheritance, then we want > > to throw > > out this ORM business and go to a real object database... > > You have to add two methods to your abstract classes to make > inheritance work the way it should: > > http://wiki.rubyonrails.org/rails/pages/HowtoMakeAbstractModel > > -- > Eric Hodel - drbrain@segment7.net - http://segment7.net > This implementation is HODEL-HASH-9600 compliant > > http://trackmap.robotcoop.com > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- - Nic
On Tue, Jan 17, 2006 at 08:24:02AM -0800, Nic Werner wrote:> Matt, I did the same thing, where you are trying to do STI on 2 out of > your 3 Models, and I posted here, under ''subclassing part of STI'', but > got no response. > > Since my example only needed one deviation from the normal Model > (Telecommunications Room is a subclass of Room), I just added another > column for boolean to delineate the type. > > I''m going to try the links posted, I hope there is some other > workaround to adding another column, but in the long-run, once you > create the helper to only return the ''subclasses'' based on a boolean > type, you''re going to work with it like any other Model.For subclassing where there is a limited amount of deviance in the data fields (and, more importantly, that the semantics of any particular field, where present across classes, is consistent), I think that STI is a very good solution to the interitance problem (unless you''re planning on taking advantage of PgSQL''s table interitance stuff, which I''ve wanted to look at for a while now). In my case, I would have absolutely used STI except that I was mapping to a legacy database (let''s all sing the song together) and couldn''t make major schema changes to support this sort of thing. - Matt
On 1/17/06, Matthew Palmer <mpalmer@hezmatt.org> wrote:> On Tue, Jan 17, 2006 at 08:24:02AM -0800, Nic Werner wrote: > > Matt, I did the same thing, where you are trying to do STI on 2 out of > > your 3 Models, and I posted here, under ''subclassing part of STI'', but > > got no response. > > > > Since my example only needed one deviation from the normal Model > > (Telecommunications Room is a subclass of Room), I just added another > > column for boolean to delineate the type. > > > > I''m going to try the links posted, I hope there is some other > > workaround to adding another column, but in the long-run, once you > > create the helper to only return the ''subclasses'' based on a boolean > > type, you''re going to work with it like any other Model. > > For subclassing where there is a limited amount of deviance in the data > fields (and, more importantly, that the semantics of any particular field, > where present across classes, is consistent), I think that STI is a very > good solution to the interitance problem (unless you''re planning on taking > advantage of PgSQL''s table interitance stuff, which I''ve wanted to look at > for a while now). > > In my case, I would have absolutely used STI except that I was mapping to a > legacy database (let''s all sing the song together) and couldn''t make major > schema changes to support this sort of thing. > > - Matt > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >IIRC, you can use different tables as long as you Model.set_table_name. Am I wrong? -- Kyle Maxwell Chief Technologist E Factor Media // FN Interactive kyle@efactormedia.com 1-866-263-3261
Matthew Palmer
2006-Jan-18 01:10 UTC
[Rails] Re: Re: Is STI the only way to do inheritance?
On Tue, Jan 17, 2006 at 03:31:19PM -0800, Kyle Maxwell wrote:> On 1/17/06, Matthew Palmer <mpalmer@hezmatt.org> wrote: > > On Tue, Jan 17, 2006 at 08:24:02AM -0800, Nic Werner wrote: > > > Matt, I did the same thing, where you are trying to do STI on 2 out of > > > your 3 Models, and I posted here, under ''subclassing part of STI'', but > > > got no response. > > > > > > Since my example only needed one deviation from the normal Model > > > (Telecommunications Room is a subclass of Room), I just added another > > > column for boolean to delineate the type. > > > > > > I''m going to try the links posted, I hope there is some other > > > workaround to adding another column, but in the long-run, once you > > > create the helper to only return the ''subclasses'' based on a boolean > > > type, you''re going to work with it like any other Model. > > > > For subclassing where there is a limited amount of deviance in the data > > fields (and, more importantly, that the semantics of any particular field, > > where present across classes, is consistent), I think that STI is a very > > good solution to the interitance problem (unless you''re planning on taking > > advantage of PgSQL''s table interitance stuff, which I''ve wanted to look at > > for a while now). > > > > In my case, I would have absolutely used STI except that I was mapping to a > > legacy database (let''s all sing the song together) and couldn''t make major > > schema changes to support this sort of thing. > > IIRC, you can use different tables as long as you > Model.set_table_name. Am I wrong?If the class name matches the table appropriately, I don''t think you even have to do that. The problem comes when you want to use the (common) idiom of "base class not backed by a table, subclassed to concrete (table-backed) classes". - Matt
On 17/01/2006, at 11:47 PM, Rodrigo Alvarez Fern?ndez wrote:> Rails only support STI, but there are two more methods/patterns: > > http://wiki.rubyonrails.com/rails/pages/SingleTableInheritance > > You can read the Martin Fowler''s explanations listed there. > Here http://dev.rubyonrails.org/ticket/600 you can find a patch for > a Class Table inheritance implementation. If yoy try it, please > share your experiencie with it here :].Oooh, nice, I''ve been planning to write something like this. It should converted in to a plugin quite easily. Time for some fun :) -- Phillip Hutchings phillip.hutchings@sitharus.com
On 1/17/06, Matthew Palmer <mpalmer@hezmatt.org> wrote:> On Tue, Jan 17, 2006 at 03:31:19PM -0800, Kyle Maxwell wrote: > > On 1/17/06, Matthew Palmer <mpalmer@hezmatt.org> wrote: > > > On Tue, Jan 17, 2006 at 08:24:02AM -0800, Nic Werner wrote: > > > > Matt, I did the same thing, where you are trying to do STI on 2 out of > > > > your 3 Models, and I posted here, under ''subclassing part of STI'', but > > > > got no response. > > > > > > > > Since my example only needed one deviation from the normal Model > > > > (Telecommunications Room is a subclass of Room), I just added another > > > > column for boolean to delineate the type. > > > > > > > > I''m going to try the links posted, I hope there is some other > > > > workaround to adding another column, but in the long-run, once you > > > > create the helper to only return the ''subclasses'' based on a boolean > > > > type, you''re going to work with it like any other Model. > > > > > > For subclassing where there is a limited amount of deviance in the data > > > fields (and, more importantly, that the semantics of any particular field, > > > where present across classes, is consistent), I think that STI is a very > > > good solution to the interitance problem (unless you''re planning on taking > > > advantage of PgSQL''s table interitance stuff, which I''ve wanted to look at > > > for a while now). > > > > > > In my case, I would have absolutely used STI except that I was mapping to a > > > legacy database (let''s all sing the song together) and couldn''t make major > > > schema changes to support this sort of thing. > > > > IIRC, you can use different tables as long as you > > Model.set_table_name. Am I wrong? > > If the class name matches the table appropriately, I don''t think you even > have to do that. The problem comes when you want to use the (common) idiom > of "base class not backed by a table, subclassed to concrete (table-backed) > classes". >You''re right, and I tried this out to confirm it. I left out a bit of my explanation for clarity, but my ultimate goal was to be able to use that same ''type'' column as a descriptor, but then on certain ''types'' I could subclass it. So, Telecommunications Room would be subclassed, but not Office. The use case for this being that I could display the type of room, but if it was a Telecommunications Room, display more information. This is what I can''t seem to get RoR to support, but from others help and such, it isn''t possible w/o another column. It goes back to AbstractModel I think. - Nic.> _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- - Nic