Eric
Eric

Reputation: 429

Adding an "Account" that has many "Users"

I'm trying to implement a top-level account that can have one or more users. My app uses Devise for authentication so I would like to keep the Signup form as part of the User model.

I believe that I've got the models set up correctly, but I am having some trouble figuring out how the registration form should work.

Here's what I've got so far...

User.rb

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable,
         :validatable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :role_ids, :as => :admin
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :account, :company

  # Association with service accounts
  has_many :service_accounts

  # Association with company account
  belongs_to :account

end

Account.rb

class Account < ActiveRecord::Base
  attr_accessible :company

  # Association with users
  has_many :users, :dependent => :destroy

end

Registrations/new.html.erb

<h2>Sign up</h2>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %>
  <%= f.error_notification %>
  <%= f.input :name, :autofocus => true, :placeholder => "Name" %>
  <%= f.input :email, :placeholder => "Email Address" %>
  <%= f.input :password, :placeholder => "Password" %>
  <%= f.input :password_confirmation, :placeholder => "Confirm Password" %>
  <%= f.button :submit, 'Sign up', :class => 'btn btn-large btn-primary' %>
<% end %>
<%= render "devise/shared/links" %>

Here is what I could use some help with

I want to add a "company" field to the above registration form. Company is a column in the Account table. When a new user registers, I'd like to create a new account for that user and set the company attribute to whatever they provided in the company field.

I'm having trouble with the code needed for both the form (to add the company field) and the controller (to create a new account and update the company field when a user submits the form).

Thanks!

Upvotes: 2

Views: 1055

Answers (1)

Tom Harrison
Tom Harrison

Reputation: 14078

I just finished working on an app with the similar requirements. The relationship looks good. It's likely that you'll want a before_save filter on User that gets the data to be associated with Account and adds/updates the association at the same time.

You should also look into accepts_nested_attributes_for, although I think it's designed to handle the more common situation where the parent (Account, in your case) is driving the creation of children (Users). I suspect you are creating a User with Devise, and either then or later creating the association, and adding associated data to the Account model.

A thing to think about is whether Company is a model of its own, in which case Users have Accounts, and Accounts have Companies. You might want to check out the recent RailsCasts on "multi-tenant" applications -- while they may not apply to your case, they did in mine, and there were a few things I hadn't thought about when managing and structuring my data when I first started the app.

Upvotes: 1

Related Questions