Ok, I have a database model similar to the following Car id Part id CarPart id car_id part_id ... how do I best model this in ARs ??? class Car < AR has_many :carparts # has information about a specific make of car. end class Part < AR has_many :carparts # has information about manufacturer # SKU # description # etc. end class CarPart < AR belongs_to :car belongs_to :part # contains data about installation date # contains data about serial number etc. end ... does that make sense? j. -- "http://ruby-lang.org -- do you ruby?" Jeff Wood
-- -- Tom Mornini On Oct 19, 2005, at 3:06 PM, Jeff Wood wrote:> Ok, I have a database model similar to the following > > Car > id > > Part > id > > CarPart > id > car_id > part_id > > ... how do I best model this in ARs ??? > > class Car < AR > has_many :carparts > > # has information about a specific make of car. > end > > class Part < AR > has_many :carparts > > # has information about manufacturer > # SKU > # description > # etc. > endCar id Part id CarPart id car_id part_id class Car < AR has_and_belongs_to_many :parts # has information about a specific make of car. end class Part < AR has_and_belongs_to_many :cars end -- -- Tom Mornini
Where is the information that states my database has that middle table? Three tables parent 1 , parent 2 , child of parent 1 and parent 2 ... ?? On 10/19/05, Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> wrote:> > -- > -- Tom Mornini > > > On Oct 19, 2005, at 3:06 PM, Jeff Wood wrote: > > > Ok, I have a database model similar to the following > > > > Car > > id > > > > Part > > id > > > > CarPart > > id > > car_id > > part_id > > > > ... how do I best model this in ARs ??? > > > > class Car < AR > > has_many :carparts > > > > # has information about a specific make of car. > > end > > > > class Part < AR > > has_many :carparts > > > > # has information about manufacturer > > # SKU > > # description > > # etc. > > end > > Car > id > > Part > id > > CarPart > id > car_id > part_id > > > class Car < AR > has_and_belongs_to_many :parts > > # has information about a specific make of car. > end > > class Part < AR > has_and_belongs_to_many :cars > end > > -- > -- Tom Mornini > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- "http://ruby-lang.org -- do you ruby?" Jeff Wood
Jeff, http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000454
Jeff, If you use the standard has_and_belongs_to_many association rails assumes a database table cars_parts with car_id and part_id. It is advisable _not_ to store an id in this table, as this can mix things up. If you''d like to specify extra methods about the join table you could turn that into a model too, but that isn''t necessary per se. If you do like to, just create the carpart model, specify wich table it uses and associate it with belongst_to :car, :part. Jaap On 10/21/05, Seth Rasmussen <seths.mailing.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Jeff, > > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000454 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Cools, thank you very much ... I hadn''t looked at the online documentation, and am only a little way into the book. Anyways, thank you all for your help. j. On 10/21/05, Jaap Schreurs <jhschreurs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Jeff, > > If you use the standard has_and_belongs_to_many association rails > assumes a database table cars_parts with car_id and part_id. It is > advisable _not_ to store an id in this table, as this can mix things > up. > > If you''d like to specify extra methods about the join table you could > turn that into a model too, but that isn''t necessary per se. If you do > like to, just create the carpart model, specify wich table it uses and > associate it with belongst_to :car, :part. > > Jaap > > On 10/21/05, Seth Rasmussen <seths.mailing.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Jeff, > > > > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000454 > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- "http://ruby-lang.org -- do you ruby?" Jeff Wood