Re: Creating Two Models in One Form … -form.html

Last edited by lumbee (2012-02-11 11:24:10)

Re: Creating Two Models in One Form

How does one do the exact opposite of this (wonderful!) tutorial? I need one model form to draw from another model and save the foreign key:

Here's my SO posting for this question: … new-object

I have two models, Story and Category. Using the form for Story#new, I'd like to be able to save the foreign key for Category.

To display the selection data from Category on the Story#new form page I've used:

<%= collection_select(:category , :category, Category.all , :id, :category, {:prompt => 'Select Category...'}) %>

How can I save category_id to the newly created Story object?Story has attributes: industry_id and user_id, Category has attribute name development.log tells me this: (it looks like it's trying to add a new category (99) to the Categories table:

Started POST "/stories" for at 2012-02-10 17:32:56 -0600
 Processing by StoriesController#create as HTML
story"=>{"industry_id"=>"8", "user_id"=>"8"}, "category"=>{"category"=>"99"}, "commit"=>"Create Story"}

Last edited by dgilliam12 (2012-02-10 20:10:16)

Re: Creating Two Models in One Form

Thanks for the post but I'd like to know how folks would do something like this with ActiveModel.To go back to the original post would you do this instead?

def create  
@task =[:task])  
@project =[:project], @task) 
redirect_to :action => 'index'  
render :action => 'new'  

or stick with the same approach but inside the Project class have a tasks method that returns a new instance of Tasks that contains a build method.  Thoughts?

Thanks in advance.

Last edited by perryjason (2012-11-09 11:33:10)