# RoR - getting started with database relations

**URL:** <https://www.ruby-forum.com/t/ror-getting-started-with-database-relations/166358>\
**Category:** Rails\
**Created:** [April 24, 2009, 9:44am UTC](https://www.ruby-forum.com/t/ror-getting-started-with-database-relations/166358 "2009-04-24T09:44:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradherman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@bradherman](https://www.ruby-forum.com/u/bradherman)\
**Post date:** [April 24, 2009, 9:44am UTC](https://www.ruby-forum.com/t/ror-getting-started-with-database-relations/166358/1 "2009-04-24T09:44:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bradherman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@bradherman](https://www.ruby-forum.com/u/bradherman)\
**Post date:** [April 24, 2009, 10:58am UTC](https://www.ruby-forum.com/t/ror-getting-started-with-database-relations/166358/2 "2009-04-24T10:58:50Z")

</div>

On 24 Apr 2009, at 06:38, removed\_email\_address@domain.invalid 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 [Active Record Associations — Ruby on Rails Guides](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
