# Getting started with database/model relationships

**URL:** <https://www.ruby-forum.com/t/getting-started-with-database-model-relationships/166349>\
**Category:** Rails\
**Created:** [April 24, 2009, 7:47am UTC](https://www.ruby-forum.com/t/getting-started-with-database-model-relationships/166349 "2009-04-24T07:47:40Z")\
**Posts on this page:** 5\
**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, 7:47am UTC](https://www.ruby-forum.com/t/getting-started-with-database-model-relationships/166349/1 "2009-04-24T07:47:40Z")

</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, 8:50am UTC](https://www.ruby-forum.com/t/getting-started-with-database-model-relationships/166349/2 "2009-04-24T08:50:23Z")

</div>

Hey Bernhard,

take a look first into this. There is a really good explanation, hope  
you come along with this.

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

Rafael

On 24 Apr., 07:47, Bradley H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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, 8:54pm UTC](https://www.ruby-forum.com/t/getting-started-with-database-model-relationships/166349/3 "2009-04-24T20:54:21Z")

</div>

I think I’m making some progress here, but what I’m wondering about now  
is whether or not my full-text search using acts\_as\_ferret will still  
work. Right now, in the old database setup I have, the text search will  
find everything in the parts table. However, lets say I link part1 with  
model1 through a model\_id (or part\_id), will searching parts for  
“model1” yield a result?

---

<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:49pm UTC](https://www.ruby-forum.com/t/getting-started-with-database-model-relationships/166349/4 "2009-04-24T22:49:43Z")

</div>

Okay… Here is what I have… Let me know if I should make any  
refinements…

[![](https://farm4.static.flickr.com/3316/3471892268_47af8b016f_o.jpg) ](https://farm4.static.flickr.com/3316/3471892268_47af8b016f_o.jpg)

class Year \< ActiveRecord::Base  
has\_many :makes  
has\_many :models, :through =\> :make  
has\_many :parts, :through =\> :model  
end

class Make \< ActiveRecord::Base  
belongs\_to :year  
has\_many :models  
has\_many :parts, :through =\> :model  
end

class Model \< ActiveRecord::Base  
belongs\_to :make  
belongs\_to :year  
has\_many :parts  
end

class Part \< ActiveRecord::Base  
belongs\_to :model  
belongs\_to :make  
belongs\_to :year  
end

---

<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 25, 2009, 7:48am UTC](https://www.ruby-forum.com/t/getting-started-with-database-model-relationships/166349/5 "2009-04-25T07:48:15Z")

</div>

Ideally I’d like a part to have a year\_id, make\_id, and model\_id. I’d  
like a model to have a single make\_id but have the option of belonging  
to several years (aka: Corvette could have years 1952 thru 2009). The  
make would belong to several years and have several makes (ie. Chevy  
from 1911-Present, and models like Corvette, Malibu, Camaro, etc…). I  
don’t know why this is confusing me so much, but any help would be  
appreciated.
