# Accounts and Users

**URL:** <https://www.ruby-forum.com/t/accounts-and-users/180876>\
**Category:** Rails\
**Created:** [December 30, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/accounts-and-users/180876 "2009-12-30T23:48:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 30, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/accounts-and-users/180876/1 "2009-12-30T23:48:26Z")

</div>

I’m currently using Authlogic to authenticate my app. I have a accounts  
and user model, where accounts has\_many :users and user belongs\_to  
:account.

I’m having trouble understanding how I should logically add this  
functionality into my app.

“User creates a new account; creating a new account and an admin  
account. User can add additional users to his account at a later time.”

I know I should have a user model. At the moment, I have it simple  
create a new record with timestamps. I then need to associate each user  
with an account\_id, where I’ve created in a new migration to add that  
column to the user table.

My routes are working, and in the console I can see user.account and  
account.users just fine. I’m stuck at how I’m supposed to add the  
creation on the new account in my user \> create method.

Anyone have any clues or can maybe point me in the right direction?

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 30, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/accounts-and-users/180876/2 "2009-12-30T23:49:48Z")

</div>

Steve C. wrote:

> “User creates a new account; creating a new account and an admin  
> account. User can add additional users to his account at a later time.”

…should actually read:

“User creates a new account; thereby creating a new account and an admin  
user. User can add additional users to his account at a later time.”

Sorry about that.

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 31, 2009, 2:11am UTC](https://www.ruby-forum.com/t/accounts-and-users/180876/3 "2009-12-31T02:11:53Z")

</div>

Steve C. wrote:

> My routes are working, and in the console I can see user.account and  
> account.users just fine. I’m stuck at how I’m supposed to add the  
> creation on the new account in my user \> create method.

There are a few ways you could accomplish this. One way would be to use  
nested routes. Make User nested under Account. That way you’ll have  
access to the account when you need to add users to the account:

POST: [http://localhost:3000/accounts/1/users](http://localhost:3000/accounts/1/users)

This could be used to add the users to the account later. For the case  
where you need to create a new Account and your first “admin” User. You  
could create both during the signup process in the accounts\_countoller  
#create action:

@account = Account.new(…account params…)  
@account.users.build(…user params…)  
@account.save

Or something to that effect.
