# One to Many example... please!

**URL:** <https://www.ruby-forum.com/t/one-to-many-example-please/54249>\
**Category:** Rails\
**Created:** [February 27, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249 "2006-02-27T20:13:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [February 27, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249/1 "2006-02-27T20:13:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [February 27, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249/2 "2006-02-27T20:22:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [February 27, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249/3 "2006-02-27T20:28:56Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [February 27, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249/4 "2006-02-27T20:55:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [February 27, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249/5 "2006-02-27T21:58:01Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [February 27, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249/6 "2006-02-27T22:30:29Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [February 27, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/one-to-many-example-please/54249/7 "2006-02-27T23:08:10Z")

</div>

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.
