frannyzooey-ee4meeAH724@public.gmane.org
2005-Feb-07 19:51 UTC
has_and_belongs_to_many, and actual db
Hello everyone : ) I have a question about many-to-many relationship. I have two model classes (so db tables), Company and Client. And I put has_and_belongs_to_many :clients to Company class. In this case, 1. I need to add company_id, client_id fields both Companies and Clients table? 2. clients_companies table is made by rails? or I should make it manualy ? 3. If latter, what fields needed by clients_companies table? Thanks for any advance.
On 7.2.2005, at 21:51, frannyzooey-ee4meeAH724@public.gmane.org wrote:> Hello everyone : ) > > I have a question about many-to-many relationship. > > > I have two model classes (so db tables), Company and Client. > And I put > > has_and_belongs_to_many :clients > > to Company class. > > > In this case, > > 1. I need to add company_id, client_id fields both Companies and > Clients table?No, you need a mapping table.> > 2. clients_companies table is made by rails? or I should make it > manualy ?You need to create it manually. See api docs (http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html#M000272) for naming conventions. You can also name the mapping table manually. In your case, clients_companies would be the name guessed by Rails.> > 3. If latter, what fields needed by clients_companies table?client_id company_id These are normal foreign key fields. You can also have any additional fields in the join table that belong to the association. You can then call them by, say, @client.companies.find(id).associationfieldX. //jarkko> > > Thanks for any advance. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
In addition to what Jarkko already said, yesterday this URL was posted to the IRC channel: [ http://curry.elitists.net/%7Escott/ARAssociations.png ] /B On Mon, 07 Feb 2005 11:51:50 -0800, frannyzooey-ee4meeAH724@public.gmane.org <frannyzooey-ee4meeAH724@public.gmane.org> wrote:> Hello everyone : ) > > I have a question about many-to-many relationship. > > I have two model classes (so db tables), Company and Client. > And I put > > has_and_belongs_to_many :clients > > to Company class. > > In this case, > > 1. I need to add company_id, client_id fields both Companies and Clients table? > > 2. clients_companies table is made by rails? or I should make it manualy ? > > 3. If latter, what fields needed by clients_companies table? > > Thanks for any advance. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Bruno Mattarollo <bruno.mattarollo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Currently in: Sydney, Australia