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?
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.
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
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.