Newbie: Select Box?

Hi All,

Please be patient I am still going through the RoR learning curve.

What I have:

Table: parts
id int(11)
pnum char(8)
desc varchar(60)
rev char(3)
ptype int(11)
created_on timestamp(14)
updated_on timestamp(14)

Table: part_types
id int(11)
ptype varchar(20)
desc varchar(60)
created_on timestamp(14)
updated_on timestamp(14)

What I need to do:

I have created the scaffold for the parts table. In the form for adding
a part I would like to have a select box that contains part_types.id,
part_types.ptype and populate the parts.ptype with the selected
part_type.id

Questions:

  1. How is the data from the part_types table selected and populated
    into the parts form?

  2. Is this a form, helper or controller method?

  3. Are there any specific database requierments, i.e. Foriegn Keys?

Thanks in advance

Richard

Is it absolutely necessary that you use these field names as you have
listed them? That is, is this a legacy table or a completely new
application? If it is completely new, then you should change the foreign
key field in your parts table so as to follow the rails conventions:

Table: parts
id int(11)
pnum char(8)
desc varchar(60)
rev char(3)
part_type_id int(11) <<<< convention to link to part_type model
created_on timestamp(14)
updated_on timestamp(14)

Then, you need to put the following into your Part.rb model:

belongs_to :part_type

and in your PartType.rb model:

has_many :parts

Once all that is in place, then in your app/views/parts/_edit.rhtml view
you could put the following:

<%= select(‘part’,‘part_type_id’,PartType.find(:all).map {|pt|
[pt.ptype, pt.id]}) %>

This will produce the select list that you want. The select() helper
wants as its third parameter an array of options of the form
[[“option”,“value”],[“option”,“value”],etc.]. The line of code above
finds and steps through all the PartTypes in the database and returns
just such an array of their ptypes and ids.

If you absolutely have to use the field you listed as the foreign key,
then in your models you would add the following to the has_many and
belongs_to declarations…

,:foreign_key => ptype

HOWEVER, I think you’re just asking for trouble, since ptype on one
table is an INT(11) and on the other table it’s a VARCHAR(20). You’re
much better off going with the convention of part_type_id for the
foreign key field.

c.

Richard wrote:

Hi All,

Please be patient I am still going through the RoR learning curve.

What I have:

Table: parts
id int(11)
pnum char(8)
desc varchar(60)
rev char(3)
ptype int(11)
created_on timestamp(14)
updated_on timestamp(14)

Table: part_types
id int(11)
ptype varchar(20)
desc varchar(60)
created_on timestamp(14)
updated_on timestamp(14)

What I need to do:

I have created the scaffold for the parts table. In the form for adding
a part I would like to have a select box that contains part_types.id,
part_types.ptype and populate the parts.ptype with the selected
part_type.id

Questions:

  1. How is the data from the part_types table selected and populated
    into the parts form?

  2. Is this a form, helper or controller method?

  3. Are there any specific database requierments, i.e. Foriegn Keys?

Thanks in advance

Richard