1

Using this question and answer ( Use both Account and User tables with Devise ) I have successfully set up for my app the ability for a user who signs up to create an account at the same time. Currently, I have two models: users and accounts. In the user model I have an account_id field.

I am struggling now with how I can make this user (ie the first one who creates the account) be defaulted as the admin. I have an admin field in my users model (this is for use with ActiveAdmin which has been set up to use a single Users Model).

Secondly, I know there are multiple posts to figure out how the admin user can create other users (which I am still trying to get to work with Devise), but can someone guide me on the easiest way so that the other users all are assigned the same account_id. I plan on using CanCan to control what the admin and non admins have access to in both ActiveAdmin and in the application in general.

Any asistance would be greatly appreciated.

My current models are:

Account Model

class Account < ActiveRecord::Base   
  has_many :users, :inverse_of => :account, :dependent => :destroy      
  accepts_nested_attributes_for :users   
  attr_accessible :name, :users_attributes
end

User Model

class User < ActiveRecord::Base
  belongs_to :account, :inverse_of => :users   
  validates :account, :presence => true
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

My controllers are:

Account Controller

class AccountsController < ApplicationController    
  def new     
    @accounts = Account.new     
    @accounts.users.build  
  end    
  def create     
    @account = Account.new(params[:account])     
    if @account.save       
      flash[:success] = "Account created"       
      redirect_to accounts_path     
    else       
      render 'new'     
    end   
  end  
end

User Controller

class UsersController < ApplicationController   
  before_filter :authenticate_user!   
  load_and_authorize_resource # CanCan
  def new     
    @user = User.new   
  end    
  def create         
    @user.skip_confirmation! # confirm immediately--don't require email confirmation     
    if @user.save       
      flash[:success] = "User added and activated."       
      redirect_to users_path # list of all users     
    else       
      render 'new'     
    end   
  end
end 
Community
  • 1
  • 1
user1648020
  • 97
  • 1
  • 12
  • Taryn: Thank you for your response. I tried Michael's suggestion below and received an error message. Since that time I have tried numerous combinations to get the server to start -- some of which were succesful -- but none of them add true to the admin field as the first user --- the comibinations I tried include **validates :admin, :in => {:in => [true]}, :if => :first?** – user1648020 Sep 13 '12 at 02:52

1 Answers1

3

If all you want to do is force the first user to be an Admin, then try this:

class Account < ActiveRecord::Base
   after_create :make_first_user_an_admin

   def make_first_user_an_admin
      return true unless self.users.present?
      self.users.first.update_attribute(:admin, true)
   end
end

It'll only run once - at the time the account is first created. I'd also recommend a validation that there are some users on the account.

Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • 1
    Taryn: Thank you for your response and the code (as you probably knew) works like a charm. This is what I was looking for and although I got something to work last night it was no where near as clean and sueful as what you posted. The next task I am set to tackle is making it so the admin can create users and those users are assigned the same account number as the admin. Wish me luck and thank you again for the assistance. – user1648020 Sep 14 '12 at 01:45