Getting started with database/model relationships

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!

Hey Bernhard,

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

http://guides.rubyonrails.org/association_basics.html

Rafael

On 24 Apr., 07:47, Bradley H. [email protected]

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?

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

http://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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs