Set forign key


#1

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?


#2

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.


#3
  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?


#4

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.


#5

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