Updating a form containing array of text fields with has_man

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:

<%= text_field 'employer', 'employer_name' %> <%= text_field 'employer', 'position' %> <%= text_field 'employer', 'months' %>

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs