Creating 2 or 3 models in one form

Anybody knows about an updated tutorial like this one: http://railsforum.com/viewtopic.php?id=717

That one was written in 2006 and some of it's code is deprecated. I have tried to update the code but I can't get it quite right yet.

If I validate_presence_of parent_id, I get error "parent_id can't be blank".

If I don't validate the presence of the parent_id, it saves fine, but in the console I can see there is two separate insert statements happening, such as... INSERT INTO "parents" ("name") VALUES ('Bob') INSERT INTO "children" ("parent_id") VALUES ('1')

So all I'm really asking is for a similar tutorial. I have been searching but can't find what I'm looking for yet. I'll keep on searching.

Anybody knows about an updated tutorial like this one:http://railsforum.com/viewtopic.php?id=717

That one was written in 2006 and some of it's code is deprecated. I have tried to update the code but I can't get it quite right yet

If I validate_presence_of parent_id, I get error "parent_id can't be blank".

If I don't validate the presence of the parent_id, it saves fine, but in the console I can see there is two separate insert statements happening, such as... INSERT INTO "parents" ("name") VALUES ('Bob') INSERT INTO "children" ("parent_id") VALUES ('1')

So all I'm really asking is for a similar tutorial. I have been searching but can't find what I'm looking for yet. I'll keep on searching.

The term you are looking to use to google for an answer is Nested Forms.

I usually search like this: rails [version] [search terms]

There is plenty of stuff out there on nested forms, checkout Railscasts episode 196 for a place to start.

HTH

Paul

That Railscast is good but it's not what I'm looking for. I don't want to dynamically add fields. And I just need to add one Account and one User that belongs to that account. As simple as that.

To make things simpler I just tried to work with ONE field, if it works then I can add more. So after a lot of experimentation and googling. This is what I came up with.

It's a form from account model. I'm trying to create a company object and adding the company name and the account_id.

_form.html.erb (account) <%= form_for @account do |account_form| %>   <p>     <% account_form.fields_for :companies do |company_form| %>       <%= company_form.label :name, 'Company name' %>       <%= company_form.text_field :name %>     <% end %>   </p>

  <div class="actions">     <%= account_form.submit %>   </div> <% end %>

Ok fixed it by changing the controller. build is already on 'new', so I removed it from 'create'.

Ok, I'm able to write to 3 models in 1 form. The code creates a brand new record in each of the three tables and writes the corresponding account_id where it's supposed to be. Everything works well, except one thing.

PROBLEM: "company_id" it's not being written in the "users" table.

"account_id" writes in the "companies" and "users" table. It's just the "company_id" that doesn't get saved.

Here are the models.

You are not going to get company_id written to the user table this way - as there is nothing to tell account - when it is creating the users that it should connect them to company too

Are these statements true ?

Account_0 => User => Account_1 : Account_0 == Account_1

Account_0 => User => Company => Account_1 : Account_0 == Account_1

Account_0 => Company => User_0…User_N => Account_1 : Account_0 == Account_1

(this last one - i mean to say - are all users of a company belonging to the same account?

you may be able to customize your companies_attributes= method in Account such that it cascades the users_attributes into the new company that it has created.

Well, this worked. It added the account_id on companies. It added the company_id on users.

It did not, however, add account_id on users. But I guess it should be ok, right? I mean, if I know which company the users belongs to, I'm still able to find out what account_id is the user from.

Remember, Account Company belongs to account User belongs to company