Rails foreign key

Hello,
I am new to rails. I have a table customer and another table
customercontact
Following is part of my tables

table customer
{
id,
name,

}

table customercontact
{
id,
customer_id,

}

I created the models and I created the scaffolds. But when I try to add
customercontact I am not getting an option to add customer_id (not even
a text box). Did this happen to anyone. If so how to resolve this.

Thanks
Dilip

Dilip wrote:

Hello,
I am new to rails. I have a table customer and another table
customercontact
Following is part of my tables

table customer
{
id,
name,

}

table customercontact
{
id,
customer_id,

}

I created the models and I created the scaffolds. But when I try to add
customercontact I am not getting an option to add customer_id (not even
a text box). Did this happen to anyone. If so how to resolve this.

Thanks
Dilip

Hi Dilip:

Scaffolding does not build the drop-downs to connect your two tables,
because there’s no way for the generator to know how you intend to
populate that dropdown. You’ll need to build the dropdown to
choose customer id yourself, using the select helper. It will look
something like this:

<%= select(‘customercontact’,‘customer_id’,Customer.find(:all).collect
{|c| [c.name,c.id]}) %>

I’d really recommend you get the Agile Web D. With Rails book
from Pragmatic Programmer (~$22 for PDF download) and work through the
tutorial. It demonstrates how to accomplish most basic functionality
like this.

c.

On 11/17/06, Cayce B. [email protected] wrote:

Scaffolding does not build the drop-downs to connect your two tables,
because there’s no way for the generator to know how you intend to
populate that dropdown.

It does if you use the Scaffolding Extensions plugin [1]. I don’t
recommend using it as a generator, but it works great as a class
method.

[1]
http://wiki.rubyonrails.com/rails/pages/Scaffolding+Extensions+Plugin

Jeremy