Forum: Ruby on Rails Ruby on Rails and lookup tables

985b2e0b3204153b3eae52416b79e38c?d=identicon&s=25 Jakob Rohde (Guest)
on 2005-12-22 21:57
(Received via mailing list)
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 <model name> <controller>"
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
137039860fa6b38713e1349da8a4137d?d=identicon&s=25 Marc Love (Guest)
on 2005-12-22 22:11
Jakob Rohde 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 <model name> <controller>"
> 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)
2073c7e0e7d2311727b680a887a375be?d=identicon&s=25 Leon Leslie (Guest)
on 2005-12-22 23:00
(Received via mailing list)
In your view  _form.rhtml

<select id="primary_table_employee_id"
name="primary_table[employee_id]">
     <%= options_from_collection_for_select(Employees.find(:all), "id",
"firstname", @primary_table.employee_id ?               i
@primary_table.employee_id : -1) %>
</select>
985b2e0b3204153b3eae52416b79e38c?d=identicon&s=25 Jakob Rohde (Guest)
on 2005-12-23 08:47
(Received via mailing list)
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
25bbc96d9c53647354cb724e744b2222?d=identicon&s=25 Greg Freemyer (Guest)
on 2005-12-23 18:08
(Received via mailing list)
On 12/22/05, Marc Love <marc.love@herbank.com> 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:
<select id="case_case_type_id" name="Case[case_type_id]">
<%= options_from_collection_for_select @case_types, "id", "case_type",
@case.case_type_id %>
</select>

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

Thanks
Greg
--
Greg Freemyer
The Norcross Group
Forensics for the 21st Century
137039860fa6b38713e1349da8a4137d?d=identicon&s=25 Marc Love (Guest)
on 2005-12-23 18:17
Greg Freemyer 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:
> <select id="case_case_type_id" name="Case[case_type_id]">
> <%= options_from_collection_for_select @case_types, "id", "case_type",
> @case.case_type_id %>
> </select>
>
> 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.
25bbc96d9c53647354cb724e744b2222?d=identicon&s=25 Greg Freemyer (Guest)
on 2005-12-23 19:38
(Received via mailing list)
On 12/23/05, Marc Love <marc.love@herbank.com> 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 Freemyer
The Norcross Group
Forensics for the 21st Century
B3c390506b3479da18ae1409e72f726d?d=identicon&s=25 Quintus (Guest)
on 2005-12-26 00:49
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.