设计用户拥有帐户,帐户有很多用户
Devise user owns account, account has many users
您好,我有一个使用 devise 进行身份验证和 devise invitable 的应用程序。
注册后用户创建了一个帐户。
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:帐户作为所有者(用户注册)和belongs_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" %>
你会推荐一个连接 table account_users account:references user:references... 还是有一个简单的方法来做到这一点?
我考虑过 Admin Devise 模型,但这让登录有点麻烦。
将 Account
作为 parent 并将 User
作为 child 会做得更好,如下所示:
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
然后当您从您的 "Admin" 帐户创建另一个用户时,只需从控制器设置当前帐户。
你甚至可以这样做:
current_account.users.create(your parameters here)
将 current_account
函数放入您的应用程序控制器中。
current_account
函数如下所示:
def current_account
return current_user.account
end
我认为你可以使用 STI 而不是让 Owner class 和 Employee class 都继承自 User 和角色 inheritance_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
您好,我有一个使用 devise 进行身份验证和 devise invitable 的应用程序。
注册后用户创建了一个帐户。
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:帐户作为所有者(用户注册)和belongs_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" %>
你会推荐一个连接 table account_users account:references user:references... 还是有一个简单的方法来做到这一点?
我考虑过 Admin Devise 模型,但这让登录有点麻烦。
将 Account
作为 parent 并将 User
作为 child 会做得更好,如下所示:
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
然后当您从您的 "Admin" 帐户创建另一个用户时,只需从控制器设置当前帐户。
你甚至可以这样做:
current_account.users.create(your parameters here)
将 current_account
函数放入您的应用程序控制器中。
current_account
函数如下所示:
def current_account
return current_user.account
end
我认为你可以使用 STI 而不是让 Owner class 和 Employee class 都继承自 User 和角色 inheritance_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