# Set forign key

**URL:** <https://www.ruby-forum.com/t/set-forign-key/94592>\
**Category:** Rails\
**Created:** [May 8, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/set-forign-key/94592 "2006-05-08T16:45:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [May 8, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/set-forign-key/94592/1 "2006-05-08T16:45:43Z")

</div>

hi,

got a small problem below and not to sure how to overcome this using  
rails

two tables involved with this projects and quotes

each project has a field quote\_id as a forign field.

When a user is ready to upload a quote they are linked to quotes/new  
from the show project page. Once the quote is uploaded how can I then  
set the quote\_id field with the correct key?

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [May 8, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/set-forign-key/94592/2 "2006-05-08T17:00:28Z")

</div>

I’m confused, does a project have many quotes or does a quote have many  
projects? I would seem to me that I project would have many quotes,  
therefore you want the quote object to have a foreign key to the  
project,  
a.k.a quote.project\_id.

If that is the case, then you need some way in your interface for the  
user  
to choose which project they are uploading the quote for. two ways of  
doing  
it:

1. Have a page that lists all projects. Next to each project, provide  
a  
link to /quote/new/1, where 1 is the project id. You could do  
/quote/new?project\_id=1 also, whatever your preference. The first one  
looks  
nicer but the second one is more explicit as to what the 1 means.

2. Have a drop-down menu on the quote/new.rhtml page where the user  
must  
select which project they are uploading a quote for.

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [May 8, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/set-forign-key/94592/3 "2006-05-08T17:16:15Z")

</div>

> 1. Have a page that lists all projects. Next to each project, provide  
> a  
> link to /quote/new/1, where 1 is the project id. You could do  
> /quote/new?project\_id=1 also, whatever your preference. The first one  
> looks  
> nicer but the second one is more explicit as to what the 1 means.

A project will only have one quote

Passing over the project id to the quote is a good idea.

The user now gets taken to quotes/new/11 for example, however then they  
submit the data, they get passed to quotes/create

how can I get the id to forward across?

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [May 8, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/set-forign-key/94592/4 "2006-05-08T17:28:55Z")

</div>

Will a quotes have many projects? Or is it just a one-to-one  
relationship  
between quote and project?

If you have a url like /quote/new/1, then in the new method of the quote  
controller, params[:id] will be equal to your project id. So you just  
simply do @quote = Quote.new(:product\_id =\> params[:id]) in the  
controller.  
Make sure the action of the form on the new.rhtml is /quote/create/1.  
Also,  
once you have created a quote, to edit is, you will do /quote/edit/1,  
but in  
that case params[:id] will be the quote.id, not quote.product\_id.

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [May 8, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/set-forign-key/94592/5 "2006-05-08T17:32:08Z")

</div>

Sorry, if that wasn’t clear, you can forward the id along by using this  
url  
in your form:

:action =\> “create”, :id =\> @quote.product\_id
