Simple two-table one-many relationship

Please help. I know the answer to this is really simple, but I can’t
figure it out.

Here’s my current setup:

mysql> desc owners;
±-----------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±-----------±------------±-----±----±--------±---------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| first_name | varchar(25) | YES | | NULL | |
| last_name | varchar(25) | YES | | NULL | |
±-----------±------------±-----±----±--------±---------------+
3 rows in set (0.00 sec)

mysql> desc computers;
±------------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±------------±-----±----±--------±---------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| service_tag | varchar(10) | YES | | NULL | |
| owner_id | int(10) | YES | MUL | NULL | |
±------------±------------±-----±----±--------±---------------+
3 rows in set (0.00 sec)

Then:
./script/generate scaffold Owner
./script/generate scaffold Computer

I then added a few owners to the DB.

Now, I want to modify computers/new such that there’s the Service Tag
entry AND a drop down menu to select an owner.

I imagine this should be really simple but I can’t figure it out.
For whatever reason when I learn a new concept I have to try it my way,
and this is an easy way for me to visualize this problem.

Maybe, if I understood the above all of the literature that I’ve read
on table relations would make a lot more sense.

Thanx a lot!!

Well…in your controller do a

@userlist = Owner.find(:all)

This will obviously put the list of owners in that variable and make it
available to the rhtml view page.

In the view page you will have to load the dropdown initially and set
the value from the selected option’s value when saved.

I’m sure there are plenty of other ways though.

Steve K. wrote:

Well…in your controller do a

@userlist = Owner.find(:all)

This will obviously put the list of owners in that variable and make it
available to the rhtml view page.

In the view page you will have to load the dropdown initially and set
the value from the selected option’s value when saved.

I’m sure there are plenty of other ways though.

@ownerlist = Owner.find(:all).collect {|t| [t.firstname + " " +
t.lastname, t.id]}

Will work straight for select_tag

<%= select_tag “field”, “owner_id”, @ownerlist %>

for the view