Разработать пользователя имеет учетную запись, учетная запись имеет много пользователей - PullRequest
1 голос
/ 11 июня 2019

Привет, у меня есть приложение, которое использует devise для аутентификации и devive invit.

При регистрации пользователь создает учетную запись.

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

Пользователь регистрируется и получает роль администратора по умолчанию при создании!

    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

Я являюсьзапутался в том, как я могу управлять пользователем has_one: учетная запись как владелец (пользователь регистрируется) и own_to: учетная запись как сотрудник (пользователь приглашен)

схема

 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" %>

Вы бы порекомендовали объединить таблицу account_users account: reference user: reference ... или есть простой способ сделать это?

Iдумал о модели Admin Devise, но это делает вход в систему немного болезненным.

Ответы [ 2 ]

2 голосов
/ 11 июня 2019

Я думаю, что вместо этого вы можете использовать STI, чтобы класс Owner и класс Employee наследовали от User, а роль - в качестве наследования_column, тогда вы можете создать полиморфную связь между ролями и учетной записью

class Employee < User
  has_one :account, as: :accountable 
end
class Owner < User
 has_one :account, as: :accountable 
end
# do the same with the other roles it gives you more flexibility to have different behaviour for every role than using only User class 
Account < ApplicationRecord
 belongs_to :accountable, polymorphic: true
end 
2 голосов
/ 11 июня 2019

Вы бы лучше, если бы Account в качестве родителя и User в качестве ребенка, вот так:

Account has_many Users

Итак, что вы могли бы сделать, это в вашей User модели создать обратный вызов для проверки наличия учетной записи и создать ее, если она пуста.

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

Затем, когда вы создаете другого пользователя из своей учетной записи «Администратор», просто установите текущую учетную запись из контроллера.

Вы могли бы даже сделать что-то вроде этого:

current_account.users.create(your parameters here)

Поместите функцию current_account в контроллер вашего приложения.

Функция current_account будет выглядеть следующим образом:

def current_account
  return current_user.account
end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...