Forum: Ruby on Rails RoR - getting started with database relations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
512e53dbf8472f0f91d485ec356ef845?d=identicon&s=25 Bradley Herman (bradherman)
on 2009-04-24 09:44
(Received via mailing list)
Hey everyone... I'm just getting my feet wet with relational databases
using RoR models.  The software I'm putting together relies on car
parts.  Originally I had the database set up as follows

create_table :parts do |t|
  t.column :year, :string
  t.column :make, :string
  t.column :model, :string
  .
  .
  .
end

However, I now realize it's much better to do it using relationships
instead.  Unfortunately, I'm not too familiar with this method and
have been reading up on it for a few hours.  I just want to post what
I think I'm supposed to be doing here and hopefully get a little
feedback as to whether I'm approaching this correctly before I start
programming it into my application...

class Year < ActiveRecord::Base
  has_many :makes
end

class Make < ActiveRecord::Base
  belongs_to :year
  has_many :models, :through => something_else
end

class Model < ActiveRecord::Base
  belongs_to :make
  has_many :parts, :through => something
end

class Part < ActiveRecord::Base
  belongs_to :model
end

However, I guess what is confusing me is that if I try to set it up
this way, if I enter a part for a 1999 Jeep Cherokee, how would I link
the part (say with part_id = 1337) to 1999, Jeep, and Cherokee?  Would
I need to refine my setup here and link the part directly to the year,
make, and model by adding belongs_to and has_many or would I simply
add a column for make_id, model_id, year_id to my
"create_table :parts" method? ie:

create_table :parts do |t|
  t.column :year_id
  t.column :make_id
  t.model :model_id
end

The other thing confusing me is the ":through => something" stuff.  In
one example I see can see creating a friendship table that links two
users, but I'm not sure how I would apply it in this case or if it is
even smart to do it this way...

Any help would be appreciated.  Thanks!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-24 10:58
(Received via mailing list)
On 24 Apr 2009, at 06:38, bradley.t.herman@gmail.com wrote:
>
> However, I guess what is confusing me is that if I try to set it up
> this way, if I enter a part for a 1999 Jeep Cherokee, how would I link
> the part (say with part_id = 1337) to 1999, Jeep, and Cherokee?  Would
> I need to refine my setup here and link the part directly to the year,
> make, and model by adding belongs_to and has_many or would I simply
> add a column for make_id, model_id, year_id to my
> "create_table :parts" method? ie:
>

well you'd take your part_id, look at that row in the parts table, see
that it has model_id 123456.
Then look at that model and then you could see from the make_id and so
on that it was a jeep etc..

>
through is for when you have a many to many relation ship, so for
example if one part can be used by several different models, it might
be appropriate to have a has many through between parts and models.
on the other hand, a given model belongs to precisely 1 make, so
belongs_to/has_many should be fine.

You may find http://guides.rubyonrails.org/association_basics.html
helpful

Another thing is that it's hard to talk about the 'best way' when
talking in generalities. The shape of the data you have and the sort
of questions you want to be able to ask about that data or things you
need to do with it are important: play around with some setups and and
arrangements. If a particular scheme makes a common task difficult/
slow, odds are it's not right for you!

Fred
This topic is locked and can not be replied to.