# Creating account and user at same time

**URL:** <https://www.ruby-forum.com/t/creating-account-and-user-at-same-time/138857>\
**Category:** Rails\
**Created:** [May 28, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/creating-account-and-user-at-same-time/138857 "2008-05-28T12:08:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [May 28, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/creating-account-and-user-at-same-time/138857/1 "2008-05-28T12:08:02Z")

</div>

hi all

I’m trying to find a the best way of creating an account and a user at  
the same time (the restful way).

When a user wants to register, he needs to create an account first. So  
he gives his account info, at the bottom of the form, he can insert his  
user details.

But how do you catch this in the controllers? First the account need to  
be created, so you submit everything to the AccountController where you  
create the account, but then the user needs to be created. So the user  
info should be given to the user controller that creats the User.

I could create them both in the Account controller, but that doesn’t  
seem so REST.  
Before I forget, an Account can have many users. That’s why I’m doing it  
like this.

Another possibility would be to split the form into 2 seperate forms, so  
that first the account needs te be created and then a seperate user form  
where the user gets created.

Some of you who have a better solution for this “problem”?

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [May 28, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/creating-account-and-user-at-same-time/138857/2 "2008-05-28T12:22:03Z")

</div>

We’re doing it exactly the same way, in this case the Accounts  
controller has to do the job.  
Split the form as you mentioned, then you shoiuld get two params, one  
params[:account], one params[:user]

in the controller you have a few options to handle this, either first  
create the account and if save works without errors, create the user.

second option would be to create (but not save) both objects, then  
validate them. Advantage here would be, that you can show all validation  
errors to the user if something goes wrong here.

> Another possibility would be to split the form into 2 seperate forms, so  
> that first the account needs te be created and then a seperate user form  
> where the user gets created.

True, from a programming view that would be the best solution. But users  
have a nasty habit to complain if they don’t understand the reason for  
two steps if they think it could be done in one step (ok, they’re only  
users, but: they pay…)

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [May 28, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/creating-account-and-user-at-same-time/138857/3 "2008-05-28T14:55:35Z")

</div>

So everything happens in you AccountController. Something like this:

```
def create
 # create_account is in Account model
 account = Account.create_account(params[:account])
  if account.save
    #create_admin is in User model
    user = User.create_admin(account, params[:user])
    if user.save
      redirect_to(resources_url())
    end
  end
 end

```

Could be the solution untill I can find a nicer way of handling it. Thx  
for the info
