Search code examples
ruby-on-railsdevise

Devise user owns account, account has many users


Hi I have an app that uses devise for authentication and devise invitable.

On sign up the user creates an account.

class Account < ApplicationRecord
  belongs_to :user, class_name: "owner", foreign_key: "owner_id"
  has_many :users, dependent: :destroy
  has_many :clients, dependent: :destroy
end

The user signs up and is given the role admin by default on create!

    class User < ApplicationRecord
  has_merit
  enum role: [:user, :tech, :admin, :manager]
  has_one :account, foreign_key: 'owner_id'
  accepts_nested_attributes_for :account
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :admin
  end

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :invitable, :registerable,
         :recoverable, :rememberable, :validatable
end

I am confused on how I can manage the user has_one :account as owner (user signs up)and belongs_to: account as employee (user is invited)

Schema

 create_table "accounts", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "owner_id", null: false
  end

create_table "users", force: :cascade 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.inet "current_sign_in_ip"
    t.inet "last_sign_in_ip"
    t.integer "role"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "sash_id"
    t.integer "level", default: 0
    t.bigint "account_id"
    t.index ["account_id"], name: "index_users_on_account_id"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

app/views/devise/registrations/new.html.erb

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :email,
                required: true,
                autofocus: true,
                input_html: { autocomplete: "email" }%>

    <%=  f.simple_fields_for :accounts do |a| %>
      <%= a.input :name %>
    <% end %>

    <%= f.input :password,
                required: true,
                hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
                input_html: { autocomplete: "new-password" } %>
    <%= f.input :password_confirmation,
                required: true,
                input_html: { autocomplete: "new-password" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

would you recommend a join table account_users account:references user:references... or is there a simple way to do this?

I thought about an Admin Devise model but that makes login a bit of a pain.


Solution

  • You would do better by having Account as the parent and User as the child like so:

    Account has_many Users
    

    So what you could do is in your User model create a callback to check for the presence of an account and create one if it's blank.

    before_validation :create_account_if_blank
    
    
    def create_account_if_blank
        if self.account.blank?
            ApplicationRecord.transaction do
                account = Account.create!(name: self.full_name)
                some_other_thing = Something.create!(name: 'test')
            end
         end
    end
    

    Then when you create another user from your "Admin" account, just set the the current account from the controller.

    You could even do something like this:

    current_account.users.create(your parameters here)
    

    Put the current_account function in your application controller.

    The current_account function would look like this:

    def current_account
      return current_user.account
    end