title: Pass Account id (parent) to User (child) with Devise in Rails
body: <p>I have set up the necessary models and views to have a Devise resource, Account and User. Account has_many users &amp; User belongs_to account. The schema reflects account_id as an attribute of user. I'm probably mistaken but I was under the impression that this account_id attribute would automatically be filled when an Account is logged in &amp; creates a new user. Upon checking the Rails console, it seems all new users created this way had a nil value for account_id. Separate question but is this the ideal way to have multitenancy in an application using Devise?</p>
  
  <p>Models:</p>
  
  <p>user.rb</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    belongs_to :account, :inverse_of =&gt; :users   
    accepts_nested_attributes_for :account
  end
  </code></pre>
  
  <p>account.rb</p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_many :users, :inverse_of =&gt; :account, :dependent =&gt; :destroy  
    accepts_nested_attributes_for :users
    has_many :projects
  
  end
  </code></pre>
  
  <p>schema.rb (just users &amp; accounts)</p>
  
  <pre><code>create_table "users", force: true do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0,  null: false
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.integer  "account_id"
      t.boolean  "supervisor"
      t.string   "name"
    end
  create_table "accounts", force: true do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0,  null: false
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.string   "name"
    end
  </code></pre>
  
  <p>users_controller.rb</p>
  
  <pre><code>class UsersController &lt; ApplicationController
      before_filter :authenticate_user! 
      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
  
      def index
          @users = User.all
      end
  
  end
  </code></pre>
  
  <p>accounts_controller.rb</p>
  
  <pre><code>class AccountsController &lt; 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
  </code></pre>
  
  <p>from registrations > edit.html.erb</p>
  
  <pre><code>&lt;% if account_signed_in? %&gt;
    &lt;div class="jumbotron"&gt;
      &lt;span&gt;&lt;%= link_to "Add user", new_user_registration_path, class: "btn btn-primary btn-sm" %&gt;&lt;/span&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>