Assuming a hierarchy something like Customer is a Person has many payment methods etc. User is a Person has may roles username password etc. Person first_name last_name etc. Is there a way to actually do that with Active Record... where Customer and User reside in different tables... I know I could do it as Customer has a Person but the idea of accessing everything as Customer.person.first_name instead of Customer.first_name really bothers me... When I first was looking at rails a couple monthes ago I thought I saw a patch to do this... but now after playing around with some trivial stuff, I can''t seem to find said patch of any information on doing a design of this sort. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hello, I guess it''s not possible. Although there is single table inheritance, which is doing something close to this, except all inherited classes are using same table with type attribute. But all relations are class based so it''s pretty flexible and I''m using it alot :-). Sorry, if it''s not what you wanted. And in your example I think that inheriting Customer from Person is good idea but User should be aggregated to persons classes which need it. If I got it right :-) -- Pepe On 18.4.2005, at 9:34, Sean T Allen wrote:> Assuming a hierarchy something like > > Customer is a Person > has many payment methods > etc. > User is a Person > has may roles > username > password > etc. > > Person > first_name > last_name > etc. > > Is there a way to actually do that with Active Record... > > where Customer and User reside in different tables... > > I know I could do it as Customer has a Person > > but the idea of accessing everything as Customer.person.first_name > instead of Customer.first_name really bothers me... > > When I first was looking at rails a couple monthes ago I thought > I saw a patch to do this... but now after playing around with > some trivial stuff, I can''t seem to find said patch of any information > on doing a design of this sort. > > > <sean.vcf>_______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 4/18/05, Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> wrote:> Assuming a hierarchy something like > > Customer is a Person > has many payment methods > etc. > > User is a Person > has may roles > username > password > etc. > > Person > first_name > last_name > etc. > > Is there a way to actually do that with Active Record... > > where Customer and User reside in different tables... > > I know I could do it as Customer has a Person > > but the idea of accessing everything as Customer.person.first_name > instead of Customer.first_name really bothers me...The problem with your model as you''ve listed it, is what about Users who are Customers? You''ll have to have two instances to represent the same Person, one for their ''customer'' role and one for their ''user'' role. Which totally negates the whole reason to have this Person class around. In fact, Customer is_a Person and User is_a Person is generally a really bad idea. Customer and User are *roles* which a Person plays, as you said, composition. While the delegation may seem like a pain, in reality it actually matches most business domains much more closely than the transmogrification that is required to move an instance from a Customer to a User and back again depending on the context in which they''re required.> When I first was looking at rails a couple monthes ago I thought > I saw a patch to do this... but now after playing around with > some trivial stuff, I can''t seem to find said patch of any information > on doing a design of this sort. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- Cheers Koz
Greetings, Using PostgreSQL with a slightly patched version of rails I have managed to create is_a relationships using Active Record. The biggest challenge is that the PostgreSQL adapter assumes that the SEQUENCE used to create unique primary key values has a name which is based on the relation name. The following patch assumes that your inheritance hierarchy is rooted at the type Entity and therefore uses its SEQUENCE. *** postgresql_adapter.rb.orig 2005-03-29 02:14:54.000000000 -0400 --- postgresql_adapter.rb 2005-04-06 13:57:22.768333080 -0300 *************** *** 111,117 **** private def last_insert_id(table, column = "id") ! sequence_name = "#{table}_#{column || ''id''}_seq" @connection.exec("SELECT currval(''#{sequence_name}'')")[0][0].to_i end --- 111,117 ---- private def last_insert_id(table, column = "id") ! sequence_name = "entities_id_seq" @connection.exec("SELECT currval(''#{sequence_name}'')")[0][0].to_i end Besides that, is_a relationships are achieved using inheritance both in the PostgreSQL tables and ActiveRecord models. One other thing to remember is that you''ll have to associate tables with your ActiveRecord models using the set_table_name directive to avoid the single-table-inheritance mechanism. Peter. Sean T Allen wrote:> Assuming a hierarchy something like > > Customer is a Person > has many payment methods > etc. > User is a Person > has may roles > username > password > etc. > > Person > first_name > last_name > etc. > > Is there a way to actually do that with Active Record... > > where Customer and User reside in different tables... > > I know I could do it as Customer has a Person > > but the idea of accessing everything as Customer.person.first_name > instead of Customer.first_name really bothers me... > > When I first was looking at rails a couple monthes ago I thought > I saw a patch to do this... but now after playing around with > some trivial stuff, I can''t seem to find said patch of any information > on doing a design of this sort. > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >