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.

On Jul 20, 2:32am, “Leonel .[email protected] wrote:

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| %>

<% account_form.fields_for :companies do |company_form| %> <%= company_form.label :name, 'Company name' %> <%= company_form.text_field :name %> <% end %>

<%= account_form.submit %>
<% end %> ---------------------------------------------------- accounts_controller.rb def new @account = Account.new @account.companies.build
respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @account }
end

end

def create
@account = Account.new(params[:account])
@account.companies.build(params[:companies])
if @account.save
redirect_to(@account, :notice => ‘User was successfully
created.’)
else
render :action => “new”
end
end

It seems like I finally accomplished what I wanted. BUUUUT! when I
submit the form, I get the Company Name field filled, but also ANOTHER
Company Name field (I have no idea where it came from) and the message
“Company name can’t be blank”.

Why does it create an EXTRA Company Name field???

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.


Account

has_many users
has_many companies
accepts_nested_attributes_for :companies
accepts_nested_attributes_for :users


Company

belongs_to account (account_id)
has_many users
accepts_nested_attributes_for :users


User

belongs_to account (account_id)
belongs_to company (company_id)


This is what I have in the accounts_controller.rb

def new
@account = Account.new
@account.companies.build
@account.users.build

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @account }
end

end

def create
@account = Account.new(params[:account])
if @account.save
redirect_to(@account, :notice => ‘User was successfully
created.’)
else
render :action => “new”
end
end

This is a simplified version of the form

<%= form_for @account do |account_form| %>

<% account_form.fields_for :users do |user_form| %>

<%= user_form.label :first_name %> <%= user_form.text_field :first_name %>

<%= user_form.label :last_name %> <%= user_form.text_field :last_name %>

<%= user_form.label :email %> <%= user_form.text_field :email %>

<%= user_form.label :username %>
<%= user_form.text_field :username %>

<% end %>

<% account_form.fields_for :companies do |company_form| %> <%= company_form.label :name, 'Company name' %> <%= company_form.text_field :name %> <% end %>

<%= account_form.submit %>
<% end %> ---------------------------------------------------------------

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


accounts_controller.rb

accounts_controller.rb
def new
@account = Account.new
@account.companies.build

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @account }
end

end

def create
@account = Account.new(params[:account])
if @account.save
redirect_to(@account, :notice => ‘User was successfully
created.’)
else
render :action => “new”
end
end

Now if I do this on the company model…

validates_presence_of :account_id

I get error: “Companies account can’t be blank”. How can I fix that?

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


accounts_controller.rb

def new
@account = Account.new
company = @account.companies.build
company.users.build

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @account }
end

end

_form.html.erb

<%= form_for @account do |account_form| %>

<% account_form.fields_for :companies do |company_form| %>

<%= company_form.label :name, 'Company name' %> <%= company_form.text_field :name %> (Or non-profit, organization, group, school, etc.)

<% company_form.fields_for :users do |user_form| %>

<%= user_form.label :first_name %> <%= user_form.text_field :first_name %>

<%= user_form.label :last_name %> <%= user_form.text_field :last_name %>

<%= user_form.label :email %> <%= user_form.text_field :email %>

<% end # fields_for :users %>
<% end # fields for :companies %>

<%= account_form.submit %>
<% end %>

On Jul 20, 2011, at 7:44 PM, Leonel . wrote:


belongs_to account (account_id)
has_many users
accepts_nested_attributes_for :users


User

belongs_to account (account_id)
belongs_to company (company_id)

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.