One to Many example... please!


#1

Hi,

I am pulling my hair out trying to work out how to put together what
should be a simple app in rails. The app is to CV’s so I have a table of
CV’s and each CV can have multiple skills. Skills are in a 2nd table
below;

CREATE TABLE cvs(
id INT not null AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) not null,
family_name VARCHAR(30) not null,
email VARCHAR(200) not null,
telephone varchar(20) not null,
address_1 varchar(30) not null,
city varchar(30) not null,
county varchar(30) not null,
post_code varchar(9) not null,
created_on timestamp(14) not null,
updated_on timestamp(14) not null
);

CREATE TABLE skills(
id INT not null AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(20) not null,
detail VARCHAR(200) not null,
cv_id int not null,
created_on timestamp(14) not null,
updated_on timestamp(14) not null,
constraint fk_skills_cv foreign key (cv_id) references cvs(id)
);

Now, I think the db is as it should be from what I have been able to
gleen from Agile…, 4 days… and OnLamp…

So now I have the view for the CV in place with a link to ‘Add a Skill’
which goes;

<%= link_to “add_skill”, :controller => “cv_admin”, :action =>
“new_skill” , :id => cv.id %>

But what should I put in the Controller & skill model to;

a. Create the skill with an association to the CV?
b. Find the skills associated with the CV so I can display them?

Just a couple of snippets would be fantastic, I just cannot find any
documentation anywhere that brings this simple concept together.

Thanks


#2

What do your models look like?

They should resemble something like this

class CV < ActiveRecord::Base
has_many :skills
end

class Skill < ActiveRecord::Base
belongs_to :cv
end


#3

Now, I think the db is as it should be from what I have been able to
gleen from Agile…, 4 days… and OnLamp…

But have you created models for your two tables yet?
script/generate model CV
script/generate model Skill

By the way, unless this is a legacy db, I would use something more
descriptive than “CV”. Not only does make your code more cryptic,
but you’ll also have to explicitly tell rails the name of your db table.

b. Find the skills associated with the CV so I can display them?

Just a couple of snippets would be fantastic, I just cannot find any
documentation anywhere that brings this simple concept together.

Thanks

Yes, but your controller can’t use your data until the models are set
up.

class CV < ActiveRecord::Base
set_table_name “cvs”
has_many :skills
end

class Skill < ActiveRecord::Base
belongs_to :cv
end

Now i can get a CV like so:
@cv = CV.find(:first)

And their associated skills as such:
@cv.skils

-Derrick S.


#4

Nithin R. wrote:

What do your models look like?

They should resemble something like this

class CV < ActiveRecord::Base
has_many :skills
end

class Skill < ActiveRecord::Base
belongs_to :cv
end

Sorry, I forgot to add this (my wife was shouting so I hurried the
submit button!)

The models are just as above with the :Foreign_key => ‘Cv_id’ on the
skill class and I have run the generate for both… so all that is in
place, but how to actually code the New & Create methods etc?


#5

Derrick S. wrote:

Now, I think the db is as it should be from what I have been able to
gleen from Agile…, 4 days… and OnLamp…

But have you created models for your two tables yet?
script/generate model CV
script/generate model Skill

By the way, unless this is a legacy db, I would use something more
descriptive than “CV”. Not only does make your code more cryptic,
but you’ll also have to explicitly tell rails the name of your db table.

b. Find the skills associated with the CV so I can display them?

Just a couple of snippets would be fantastic, I just cannot find any
documentation anywhere that brings this simple concept together.

Thanks

Yes, but your controller can’t use your data until the models are set
up.

class CV < ActiveRecord::Base
set_table_name “cvs”
has_many :skills
end

class Skill < ActiveRecord::Base
belongs_to :cv
end

Now i can get a CV like so:
@cv = CV.find(:first)

And their associated skills as such:
@cv.skils

-Derrick S.

Thanks Derrick,

I see… but, if I have;

Cv.id = 1
Cv.id = 2

And;

Skill.id = 1
Skill.id = 2
skill.id = 3
skill.id = 4

how; when I add a skill do I make it associated with a particular cv ?

btw… thank you all for such quick responses…


#6

skill.id = 4

how; when I add a skill do I make it associated with a particular cv ?

btw… thank you all for such quick responses…

Here’s a cardinal rule to live by: never set/change the id on a
model object. Of course there are exceptions, but for most projects
you don’t need to manage the id’s yourself. Rails will do this for
you. With that said…

Create a Cv and set some fields

@cv = Cv.new()
@cv.myattr1 = “The value”
@cv.myattr2 = “The other value”
@cv.save

That last line actually performs the database INSERT, and an id will
be assigned. Now lets create a skill for this cv.

Create a skill

@cv.skills << Skill.new( :myattr1 => “Skillful value”, :myattr2 =>
“more skillful values” )
@cv.save

The beauty of active record is that the id’s and foreign keys are
taken care of for you! That last line of code not only inserts into
the skills table, but also sets the Cv_id foreign key to the id of @cv

You mentioned that you had the Agile book. I strongly suggest you
take a close look at chapter 14 in order to understand this further.
The particulars of when things are saved and how foreign keys are
assigned is explained very nicely.

Good luck, and welcome to Rails!

-Derrick S.


#7

Derrick S. wrote:

Create a Cv and set some fields

@cv = Cv.new()
@cv.myattr1 = “The value”
@cv.myattr2 = “The other value”
@cv.save

That last line actually performs the database INSERT, and an id will
be assigned. Now lets create a skill for this cv.

Create a skill

@cv.skills << Skill.new( :myattr1 => “Skillful value”, :myattr2 =>
“more skillful values” )
@cv.save

You mentioned that you had the Agile book. I strongly suggest you
take a close look at chapter 14 in order to understand this further.
The particulars of when things are saved and how foreign keys are
assigned is explained very nicely.

Good luck, and welcome to Rails!

-Derrick S.

Fan-tas-tic. Thank you Derrick.