Forum: Ruby on Rails Updating a form containing array of text fields with has_man

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2006-04-24 22:01
(Received via mailing list)
I've got a User class and an Employer class set up as follows:

User.rb:
has_many :employers

Employer.rb
belongs_to :user


My tables are as follows:

mysql> select id,first_name,last_name from  users limit 5;
+-----+------------+-----------+
| id  | first_name | last_name |
+-----+------------+-----------+
| 711 | Patrick    | Stewart     |
| 714 | Larry   | Davis   |


mysql> select * from employers limit 5;
+----+----------+--------------------+--------------------------+--------+
| id | user_id | employer_name      | position                 | months |
+----+----------+--------------------+--------------------------+--------+
|  1 |      711 | Test Company        | Administrative Assistant |      12 |
|  2 |      711 | Company 2             | Recruiter                |
15|
|  3 |      711 | Apple                  | Developer      |      0 |
|  4 |      711 | test                      | test position
|      0 |
|  5 |      714 | Google   | programmer         |      0 |
+----+----------+--------------------+--------------------------+--------+

I'd like to provide a single form with an array of fields for
employer_name,
position and months, so that I can allow a user to update/create their
employer information, but I can't figure out how to get rails to
automagically create the associations between the user object and the
employer object.  Right now my form partial looks like this:

_employers.rhtml:
<tr>
     <td><%= text_field 'employer', 'employer_name'  %></td>
     <td><%= text_field 'employer', 'position' %></td>
     <td><%= text_field 'employer', 'months' %></td>
</tr>

but I need some way to add the user's id to the employer record when
it's
created.  I was hoping I could do something like this:

<%= text_field 'user', 'employer[employer_name][]' %>

but it doesn't work.. (undefined method `employer[employer_name][]' for
#<User:0x888595c>).  So it looks like I may have to take care of the
associations and record updating manually, but I just thought I'd check
with
the list to see if there might be something I'm missing.

if anyone has any suggestions, please let me know.  Thanks,

Mike
This topic is locked and can not be replied to.