Ruby Rails - 资源范围 current_account
Ruby on Rails - Scoping resources by current_account
我是 rails 上 ruby 的新手,我对我正在构建的多租户应用程序的最佳下一步是什么有点困惑。
基本上我想通过 account_id 来限定资源范围,所以我在我的帐户 base_controller.
中创建了一个名为 current_account 的方法和助手
但是,教程中我关注的范围 current_account 是我不想做的子域。所以我需要一种方法来识别当前用户的account_id,这样我就可以拥有一个资源变量@contact = current_account.contacts."all".
我是否需要在用户和帐户模型之间建立新的关联,以便我可以使用 current_user 助手来定义当前帐户 ID,或者是否有更好的方法?如果是这样,最好的方法是什么?
背景
第一个注册的用户成为帐户所有者。然后帐户所有者可以邀请其他用户加入该帐户。我正在使用这个装置 gem。资源按帐户划分范围,因此只有链接到帐户的用户才能看到属于该帐户的记录。
基地控制器
module Accounts
class BaseController < ApplicationController
def current_account
@current_account ||= ?????
end
helper_method :current_account
def owner?
current_account.owner == current_user
end
helper_method :owner?
end
end
联系人(我的资源)控制器
module Accounts
class ContactsController < Accounts::BaseController
def index
@contact = current_account.contacts.all
end
end
end
账户模型
class Account < ActiveRecord::Base
belongs_to :owner, class_name: "User"
accepts_nested_attributes_for :owner
validates :subdomain, presence: true, uniqueness: true
has_many :contacts
has_many :invitations
has_many :memberships
has_many :users, through: :memberships
end
邀请模型
class Invitation < ActiveRecord::Base
belongs_to :account
validates :email, presence: true
end
会员模式
class Membership < ActiveRecord::Base
belongs_to :account
belongs_to :user
end
用户模型
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
路线
Rails.application.routes.draw do
devise_for :users
scope module: "accounts" do
resources 'dashboard'
resources 'contacts'
resources :invitations, only: [:new, :create] do
member do
get :accept
patch :accepted
end
end
resources :users, only: [:index, :destroy]
end
架构
ActiveRecord::Schema.define(version: 20170124002015) do
create_table "accounts", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "owner_id"
t.string "subdomain"
end
add_index "accounts", ["subdomain"], name: "index_accounts_on_subdomain"
create_table "contacts", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "phone"
t.string "email"
t.text "comments"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "account_id"
end
add_index "contacts", ["account_id"], name: "index_contacts_on_account_id"
create_table "invitations", force: true do |t|
t.string "email"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "token"
end
add_index "invitations", ["account_id"], name: "index_invitations_on_account_id"
add_index "invitations", ["token"], name: "index_invitations_on_token"
create_table "memberships", force: true do |t|
t.integer "account_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "memberships", ["account_id"], name: "index_memberships_on_account_id"
add_index "memberships", ["user_id"], name: "index_memberships_on_user_id"
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"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
用户和帐户之间有两种可能的关联:
- 用户有多个账户
- 用户属于一个帐户
第一种情况,无法从current_user设置租户,因为不清楚应该使用哪个帐户作为当前租户。 schema.rb 中的成员资格 table 表示这是您提到的教程采用的方法。按子域加载帐户有助于指定应将哪个帐户用作当前租户。
在第二种情况下,每个用户只有一个帐户。用户获得 account_id,成员资格 table 已过时,您可以像这样加载当前租户:
def current_account
@current_account ||= current_user.account
end
Do I need to make a new association between the user and account model
so that I can use the current_user helper to define the current
account id or is there a better way? If so, what is the best way to do
this?
在我看来,您想采用第二种方法,这需要一个帐户 has_many
个用户和一个用户 belongs_to
个帐户。
我是 rails 上 ruby 的新手,我对我正在构建的多租户应用程序的最佳下一步是什么有点困惑。
基本上我想通过 account_id 来限定资源范围,所以我在我的帐户 base_controller.
中创建了一个名为 current_account 的方法和助手但是,教程中我关注的范围 current_account 是我不想做的子域。所以我需要一种方法来识别当前用户的account_id,这样我就可以拥有一个资源变量@contact = current_account.contacts."all".
我是否需要在用户和帐户模型之间建立新的关联,以便我可以使用 current_user 助手来定义当前帐户 ID,或者是否有更好的方法?如果是这样,最好的方法是什么?
背景 第一个注册的用户成为帐户所有者。然后帐户所有者可以邀请其他用户加入该帐户。我正在使用这个装置 gem。资源按帐户划分范围,因此只有链接到帐户的用户才能看到属于该帐户的记录。
基地控制器
module Accounts
class BaseController < ApplicationController
def current_account
@current_account ||= ?????
end
helper_method :current_account
def owner?
current_account.owner == current_user
end
helper_method :owner?
end
end
联系人(我的资源)控制器
module Accounts
class ContactsController < Accounts::BaseController
def index
@contact = current_account.contacts.all
end
end
end
账户模型
class Account < ActiveRecord::Base
belongs_to :owner, class_name: "User"
accepts_nested_attributes_for :owner
validates :subdomain, presence: true, uniqueness: true
has_many :contacts
has_many :invitations
has_many :memberships
has_many :users, through: :memberships
end
邀请模型
class Invitation < ActiveRecord::Base
belongs_to :account
validates :email, presence: true
end
会员模式
class Membership < ActiveRecord::Base
belongs_to :account
belongs_to :user
end
用户模型
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
路线
Rails.application.routes.draw do
devise_for :users
scope module: "accounts" do
resources 'dashboard'
resources 'contacts'
resources :invitations, only: [:new, :create] do
member do
get :accept
patch :accepted
end
end
resources :users, only: [:index, :destroy]
end
架构
ActiveRecord::Schema.define(version: 20170124002015) do
create_table "accounts", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "owner_id"
t.string "subdomain"
end
add_index "accounts", ["subdomain"], name: "index_accounts_on_subdomain"
create_table "contacts", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "phone"
t.string "email"
t.text "comments"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "account_id"
end
add_index "contacts", ["account_id"], name: "index_contacts_on_account_id"
create_table "invitations", force: true do |t|
t.string "email"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "token"
end
add_index "invitations", ["account_id"], name: "index_invitations_on_account_id"
add_index "invitations", ["token"], name: "index_invitations_on_token"
create_table "memberships", force: true do |t|
t.integer "account_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "memberships", ["account_id"], name: "index_memberships_on_account_id"
add_index "memberships", ["user_id"], name: "index_memberships_on_user_id"
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"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
用户和帐户之间有两种可能的关联:
- 用户有多个账户
- 用户属于一个帐户
第一种情况,无法从current_user设置租户,因为不清楚应该使用哪个帐户作为当前租户。 schema.rb 中的成员资格 table 表示这是您提到的教程采用的方法。按子域加载帐户有助于指定应将哪个帐户用作当前租户。
在第二种情况下,每个用户只有一个帐户。用户获得 account_id,成员资格 table 已过时,您可以像这样加载当前租户:
def current_account
@current_account ||= current_user.account
end
Do I need to make a new association between the user and account model so that I can use the current_user helper to define the current account id or is there a better way? If so, what is the best way to do this?
在我看来,您想采用第二种方法,这需要一个帐户 has_many
个用户和一个用户 belongs_to
个帐户。