Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:30:45+00:00 2026-06-11T19:30:45+00:00

Using this question and answer ( Use both Account and User tables with Devise

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:30:46+00:00Added an answer on June 11, 2026 at 7:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this answer , I created a sample localized app. My question is, is
I have tried to find the answer to this question on both the Spring
I am trying to integrated linkedIn using this question answer Posting LinkedIn message from
Im using the answer to this question to convert an array of XML to
I am using the accepted answer from this question . The relevant code is
An answer to this question has led me to look into using Event Tracing
I'm using the accepted answer to this question to help me build an HTML
I'm using the first answer to this question Overcoming Python's limitations regarding instance methods
So I saw an answer in another question saying that this should work: using
Based on the answer I received on this question ( https://stackoverflow.com/questions/1911969/... ) I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.