Work with two tables in one controller

I want to have someone sign up for an account. Two models: Account and
User.

So an account will be created. A user will be created. The account’s id,
will be placed in the account_id column of the users table. So both, the
account and the user HAVE to be created.

So far this is what I have, but it doesn’t look quite right. Can someone
please help me refine this code?

Is there a way to rollback in case the account is created but the user
fails to be created?

If you are wondering why I need the Account model, it’s because more
users can be added to the same account.

def new

create account first, get account id

@account = Account.new
if @account.save
@user = User.new
# Newly created account id
@user.account_id = @account.id

respond_to do |format|
  if @user.save
   format.html{redirect_to(welcome_path(@user),:notice =>

‘User created.’)}
else
format.html { render :action => “new” }
end
end
else
# what to do in case @account.save fails
end

Ok, so based on this tutorial http://railsforum.com/viewtopic.php?id=717
this is what I came up with…

class Users::RegistrationsController < Devise::RegistrationsController

def new
@account = Account.new
@user = User.new
end

def create
@account = Account.new(params[:account])
@user = @account.users.build(params[:user])
# Account Owner role
@user.role_id = 3

if @account.save
  redirect_to :action => new_user_session, :notice => 'Email 

confirmation required’
else
render :action => ‘new’
end
end

end

But it will still not save account_id in the users table. I understand
why though, it’s supposed to save Account first, so there can be an
account id. Any suggestions?

Please help, I’ve been trying to do this since yesterday. I’m stuck.
With the above code I get error “Account can’t be blank”.

I have tried in so many ways and can’t get it to work. I did something
yesterday that made the app have an redirect loop (too many redirects)
and it took me over an hour just to fix it. So I’m just going to start
all over again.

My initial question had to do with other models but it’s basically the
same. I need to do the same for several models. This what I have now.

There are two models: Company and User.
company has_many :users
user has_one :company

routes.rb
resources :companies
resources :users
resources :companies do
resources :users
end

Those routes allow me to do this:
companies/2/users/new

Which is good, but once I submit the form, the app seems to forget the
company_id.

On Jul 14, 2011, at 1:58 PM, Leonel . wrote:

user has_one :company

Which is good, but once I submit the form, the app seems to forget the
company_id.

You have both a regular route and a nested route for your users. Make
sure your form is submitting to the nested route.
It should look something like (depending on your variable names):

form_for [@company, @user] do |f|

Without the company your form will post to /users and the company id
will not be passed.

Juan Alvarado

Posting some code or details about what you are trying to do will allow
us
to help with your problem.

You have both a regular route and a nested route for your users. Make
sure your form is submitting to the nested route.
It should look something like (depending on your variable names):

form_for [@company, @user] do |f|

Without the company your form will post to /users and the company id
will not be passed.

I only want to create a new User not a new Company. The Company is
pre-existent. A lot of tutorials I found don’t explain how to do that,
they explain how to create the Parent and Child at the same time. I
ALREADY have the Parent, I just want to create a Child for it, and have
the id of the parent written into the Child table, like parent_id.

How would I make the results get written into a user record? Yesterday
when I was trying to work on it, I read something about a “build”
method. Is that what you recommend I use?

On 14 July 2011 18:58, Leonel . [email protected] wrote:

I have tried in so many ways and can’t get it to work. I did something
yesterday that made the app have an redirect loop (too many redirects)
and it took me over an hour just to fix it. So I’m just going to start
all over again.

Not solving the basic problem, but a suggestion for a way of working
that can save a lot of time. Use a Source Control System (git is my
favourite) for your source and commit the code regularly (several
times an hour sometimes, dependant on what you are doing). Then when
you accidentally mess something up and cannot work out what you did it
is easy to go back to previous versions and get it working again. It
may seem like a significant learning curve when you want to get on and
develop your app, but it will save you a huge amount of time in the
long run.

Colin

On Jul 14, 2011, at 3:07 PM, Leonel . wrote:

pre-existent.
Using the company in the form_for doesn’t mean you are creating a new
company as well. That is just generating the nested url to submit the
form to. Leaving out the @company will mean that the form get submitted
to /users instead of /companies/:company_id/users. Which if I
understood you correctly is where you want to submit your form to so
that you will have the company id in your params list.

A lot of tutorials I found don’t explain how to do that,
they explain how to create the Parent and Child at the same time. I
ALREADY have the Parent, I just want to create a Child for it, and have
the id of the parent written into the Child table, like parent_id.

How would I make the results get written into a user record? Yesterday
when I was trying to work on it, I read something about a “build”
method. Is that what you recommend I use?

You use build on an association to get a new instance of that
association.
In your users controller you should have something like:

def create
@company = Company.find params[:company_id]
@user = @company.users.build params[:user]


end

Using the build on the users association will automatically set the
parent id. It is the same as doing:

@user = User.new params[:user]
@user.company_id = @company_id

and setting any other conditions you have specified on the association
manually.

Juan