Ruby on Rails and lookup tables


#1

Hi,

I’m a newbie on both Ruby and Ruby on Rails, and I’m struggling with
my first app. I have a table with a column called “employee_id” which
is a foreing key associating it with the “employees” table. I have
populated both tables.

When I run "ruby script/generate scaffold "
it generates a web page with all fields except the foreign key field.
I had hoped it would make i.e. a drop down with data from the
employees table.

How do I do this?

I’m using MySQL 5.0 on WindowsXP.

TIA,

Jakob


#2

Jakob R. wrote:

Hi,

I’m a newbie on both Ruby and Ruby on Rails, and I’m struggling with
my first app. I have a table with a column called “employee_id” which
is a foreing key associating it with the “employees” table. I have
populated both tables.

When I run "ruby script/generate scaffold "
it generates a web page with all fields except the foreign key field.
I had hoped it would make i.e. a drop down with data from the
employees table.

How do I do this?

I’m using MySQL 5.0 on WindowsXP.

TIA,

Jakob

You need to create an option form field for your view. Using the rails
helper method of options_from_collection_for_select.

In your partial template, you’re going to put:

<%= options_from_collection_for_select @employees, “id”, “name”,
@project.employee_id %>

Just remember you’re going to have to define @employees in your project
controller with something like @employees = Employee.find(:all)


#3

In your view _form.rhtml


<%= options_from_collection_for_select(Employees.find(:all), “id”,
“firstname”, @primary_table.employee_id ? i
@primary_table.employee_id : -1) %>


#4

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#5

On 12/22/05, Marc L. removed_email_address@domain.invalid wrote:

Butting in as anothoer newbie: I tried to follow the above advice for
a pull-down of my own. I’m trying to select/populate my case_type_id
field. On my edit form I now have a populated pull down that
reflects my case_type table.

It also initially has default selection that is derived from the
database. So I have a lot of functionality.

What doesn’t work is modifying the actual database filed. ie. my
case_type_id field in the database is not being updated based on the
pulldown.

Any ideas? My code looks like:

<%= options_from_collection_for_select @case_types, “id”, “case_type”,
@case.case_type_id %>

I have not added any code to extract the selection, but I assume that
is somehow automatic?

Thanks
Greg

Greg F.
The Norcross Group
Forensics for the 21st Century


#6

Greg F. wrote:

What doesn’t work is modifying the actual database filed. ie. my
case_type_id field in the database is not being updated based on the
pulldown.

Any ideas? My code looks like:

<%= options_from_collection_for_select @case_types, “id”, “case_type”,
@case.case_type_id %>

I have not added any code to extract the selection, but I assume that
is somehow automatic?

Thanks
Greg

Yes, it is automatic. The problem lies somewhere in the name for your
select tag. My initial guess is that its because you’re capitalizing
“Case”. It should read “case[case_type_id]”. Also, just double
checking, but is the row in your table actually called “case_type_id” or
is it just “type_id”? What’s in the brackets should match exactly what
your row is called.


#7

On 12/23/05, Marc L. removed_email_address@domain.invalid wrote:

is it just “type_id”? What’s in the brackets should match exactly what
your row is called.

Thanks, not sure how it was working but monkeying with the above and
changing some variable names I got an error that my list was not being
declared in the update method. Doing so caused it all to work.

Thanks
Greg

Greg F.
The Norcross Group
Forensics for the 21st Century


#8

I have a problem with very similar issue:

I have ‘employees’ and ‘assignments’ table.
In ‘assignments’ table I have an ‘employee_id’ field.
There are separate fields for first name and last name in the
‘employees’ table.

So when adding an assignment I select an employee from drop down list.
In the ‘options_from_collection_for_select’ method I specify the field
to be displayed on the list (first name or last name):
<%= options_from_collection_for_select @employees, :id, “last_name”,
@assignment.employee_id %>

But is it possible to somehow concatenate the first name and last name
fields so the full name of each employee is displayed on the list?

Doing something like:
<%= options_from_collection_for_select @employees, :id, “first_name” +
“last_name”, @assignment.employee_id %>

didn’t work.

Do You have any solution?

Greetings,
Quintus