【问题标题】:Devise user owns account, account has many users设计用户拥有账户,账户有很多用户
【发布时间】:2019-06-12 04:37:34
【问题描述】:

您好,我有一个应用程序使用设计进行身份验证并设计邀请。

用户在注册时创建一个帐户。

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 感到困惑:作为所有者的帐户(用户注册)和属于_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:references user:references... 还是有一种简单的方法可以做到这一点?

我想过一个 Admin Devise 模型,但这让登录有点痛苦。

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    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
    

    【讨论】:

    • 一个问题,我可以在设计注册期间创建父级吗?
    • 这就是你在上面的代码中所做的。您不是直接从设计控制器创建父级,而是从User 模型中创建父级。这很好,因为它将逻辑排除在控制器之外。这也很好,因为如果您的 UserAccount 验证失败,则两者都不会被创建。因此,不要在设计控制器中创建帐户,而是在“用户”模型的回调中创建它,并使用我在问题(回调)中发布的逻辑来破译何时创建(或不创建)父帐户.有意义吗?
    • 我已经更新了问题以包含表单...考虑到这一点 account = Account.create!(name: name)
    【解决方案2】:

    我认为你可以使用 STI 来让 Owner 类和 Employee 类都继承自 User 和 role 作为继承列,然后你可以在角色和帐户之间建立多态关系

    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 
    

    【讨论】:

    猜你喜欢
    • 2018-10-18
    • 1970-01-01
    • 2018-08-23
    • 2019-06-17
    • 2014-08-27
    • 2012-11-13
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多