I am sure some of you can give me an insight into this. This is more towards the database design for the scenario below: Say for example, I have a person table and this person can have different address types. One could be Home and the other could be say Office. Should be model this Table people id fname lname Table addresses id person_id addr1 addr2 .... or Table people id fname lname Table addresses id addr1 addr2 Table persons_addresses person_id address_id Are there any tutorial that explains these concepts? Any help is highly appreciated. Thanks Silvy Mathews -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060109/bf2a8ae0/attachment.html
Hi, silvy, there should be myriads of tutorials about database design in the web. Search for "database design" "normalization" and you''ll find them. Regarding your question: It depends: Your first example models a one-to-many relationship and would be fine if a duplication of addresses of people sharing the same address is ok for you. Lets say you''ve got the brothers J.J. and J.R. Smith living together at Nr. 10 FooWay, than you have to put this address twice in your address table. Because your address with id=1 could obviously reference only ONE Person in its foreign_key (person_id). Things are different in a many-to-many relationship (your second example). People there may have many addresses as well as addresses might reference many people. n:n-Relationships come with the overhead of the join-table (person_id, address_id) but provide you with the best normalization in your example (IMHO). Regards Jan Silvy@funmail.com wrote:> I am sure some of you can give me an insight into this. This is more > towards the database design for the scenario below: > Say for example, > > I have a person table and this person can have different address > types. One could be Home and the other could be say Office. > Should be model this > > Table people > id > fname > lname > > Table addresses > id > person_id > addr1 > addr2 > .... > > > or > > Table people > id > fname > lname > > Table addresses > id > addr1 > addr2 > > Table persons_addresses > person_id > address_id > > > Are there any tutorial that explains these concepts? > Any help is highly appreciated. > > Thanks > Silvy Mathews > > > > >------------------------------------------------------------------------ > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >
Hi Jan, Thanks for your reply. My concern is the best practices for doing this inside Rails. Also I was looking for exmaple code for doing this in Rails and not the schema design Thanks Silvy -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Jan Prill Sent: Monday, January 09, 2006 3:15 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Design Question Hi, silvy, there should be myriads of tutorials about database design in the web. Search for "database design" "normalization" and you''ll find them. Regarding your question: It depends: Your first example models a one-to-many relationship and would be fine if a duplication of addresses of people sharing the same address is ok for you. Lets say you''ve got the brothers J.J. and J.R. Smith living together at Nr. 10 FooWay, than you have to put this address twice in your address table. Because your address with id=1 could obviously reference only ONE Person in its foreign_key (person_id). Things are different in a many-to-many relationship (your second example). People there may have many addresses as well as addresses might reference many people. n:n-Relationships come with the overhead of the join-table (person_id, address_id) but provide you with the best normalization in your example (IMHO). Regards Jan Silvy@funmail.com wrote:> I am sure some of you can give me an insight into this. This is more > towards the database design for the scenario below: > Say for example, > > I have a person table and this person can have different address > types. One could be Home and the other could be say Office. > Should be model this > > Table people > id > fname > lname > > Table addresses > id > person_id > addr1 > addr2 > .... > > > or > > Table people > id > fname > lname > > Table addresses > id > addr1 > addr2 > > Table persons_addresses > person_id > address_id > > > Are there any tutorial that explains these concepts? > Any help is highly appreciated. > > Thanks > Silvy Mathews > > > > >----------------------------------------------------------------------- >- > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, Silvy, ok. That is as easy as class Person < ActiveRecord::Base has_many :addresses [ has_and_belongs_to_many :addresses ] end and class Address < ActiveRecord::Base belongs_to :person [ has_and_belongs_to_many :persons ] end but there is quite some material in each and every tutorial (and the introduction video) you''ll find on http://www.rubyonrails.org/docs . rails - in contrast to many other open source projects - is very well documented. I suggest you work through some of these! Regards Jan Silvy@funmail.com wrote:>Hi Jan, >Thanks for your reply. My concern is the best practices for doing this >inside Rails. Also I was looking for exmaple code for doing this in >Rails and not the schema design > >Thanks >Silvy > >-----Original Message----- >From: rails-bounces@lists.rubyonrails.org >[mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Jan Prill >Sent: Monday, January 09, 2006 3:15 PM >To: rails@lists.rubyonrails.org >Subject: Re: [Rails] Design Question > >Hi, silvy, > >there should be myriads of tutorials about database design in the web. >Search for "database design" "normalization" and you''ll find them. > >Regarding your question: > >It depends: > >Your first example models a one-to-many relationship and would be fine >if a duplication of addresses of people sharing the same address is ok >for you. Lets say you''ve got the brothers J.J. and J.R. Smith living >together at Nr. 10 FooWay, than you have to put this address twice in >your address table. Because your address with id=1 could obviously >reference only ONE Person in its foreign_key (person_id). Things are >different in a many-to-many relationship (your second example). People >there may have many addresses as well as addresses might reference many >people. n:n-Relationships come with the overhead of the join-table >(person_id, address_id) but provide you with the best normalization in >your example (IMHO). > >Regards >Jan > >Silvy@funmail.com wrote: > > > >>I am sure some of you can give me an insight into this. This is more >>towards the database design for the scenario below: >>Say for example, >> >>I have a person table and this person can have different address >>types. One could be Home and the other could be say Office. >>Should be model this >> >>Table people >>id >>fname >>lname >> >>Table addresses >>id >>person_id >>addr1 >>addr2 >>.... >> >> >>or >> >>Table people >>id >>fname >>lname >> >>Table addresses >>id >>addr1 >>addr2 >> >>Table persons_addresses >>person_id >>address_id >> >> >>Are there any tutorial that explains these concepts? >>Any help is highly appreciated. >> >>Thanks >>Silvy Mathews >> >> >> >> >>----------------------------------------------------------------------- >>- >> >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >