Rails Multiple Models per Form, Optional FK Association

Posted by ckarbass on Stack Overflow See other posts from Stack Overflow or by ckarbass
Published on 2010-04-18T00:55:52Z Indexed on 2010/04/18 1:03 UTC
Read the original article Hit count: 362

Given the following pseudo-code:

Company
has_many :jobs

Job
belongs_to :company

I'm creating a form to post a new job. In the form, I want to have two fields for an optional company. On submission, if a company was entered, I want to either create or update the company and associate it with the new job. I know if the company exists by searching the companies table for the company's url.

Is it possible to do this using form_for, fields_for, and accepts_nested_attributes_for given the company may not exist?

© Stack Overflow or respective owner

Related posts about rails

Related posts about ruby