# Associate HABTM Model on Create? Passing in a user\_id?

**URL:** <https://www.ruby-forum.com/t/associate-habtm-model-on-create-passing-in-a-user-id/175881>\
**Category:** Rails\
**Created:** [September 22, 2009, 5:12am UTC](https://www.ruby-forum.com/t/associate-habtm-model-on-create-passing-in-a-user-id/175881 "2009-09-22T05:12:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [September 22, 2009, 5:12am UTC](https://www.ruby-forum.com/t/associate-habtm-model-on-create-passing-in-a-user-id/175881/1 "2009-09-22T05:12:53Z")

</div>

Hi,

Could anyone help me pass a user\_id when creating a child record with a  
HABTM relationship? I’m basically trying to create a record from the  
following url

/users/:id/tasks/new

I tried several variants of the following, but keep getting various  
errors. My first attempt below yeilds ‘invalid Users’ error and the  
second yields a type mismatch error “user expected got string.”

\<% form\_for(@tasks) do |f| %\>  
#things I tried:  
\<%= hidden\_field\_tag “tasks[user\_ids][]”, params[:user\_id] %\>  
\<%= f.hidden\_field :user\_ids, params[:user\_id] %\>  
\<%= f.submit ‘Create’ %\>  
\<% end %\>

I get why I’m getting these errors, but haven’t been able to figure out  
the correct syntax to get it to pass through. perhaps I’ve been making  
the wrong assumption that if I could pass the user\_id to my  
create\_action, rails would understand that it needed to create the HABTM  
between the new record and the user\_id I’m referencing.

Any help would be appreciated.

Thanks!

-MG
