繁体   English   中英

使用 rails 控制台和 Apartment (Rails Multitenancy) 选择模型数据

[英]Select model data using rails console and Apartment (Rails Multitenancy)

我已经使用 Apartment for Rails 创建了一个多租户基础应用程序。 现在我无法理解如何通过 rails 控制台访问我的数据。

以下是该应用程序的工作方式:您注册后,它会将您重定向到您的子域。 登录时,您可以访问该应用程序。 你可以创建一个公司。 公司可以有很多客户。 客户属于公司。 公司归用户所有。 从这里开始,一切正常。 我想了解的是如何通过 rails 控制台访问我创建的数据? 我的应用程序运行良好,注销/登录后我的数据仍然存在。

现在,我使用user = User.first选择我的用户。 它返回我的用户。 现在,当我使用user.companies它返回以下内容:

Company Load (0.1ms)  SELECT "companies".* FROM "companies" WHERE "companies"."user_id" = ? LIMIT ?  [["user_id", 3], ["LIMIT", 11]]
 => #<ActiveRecord::Associations::CollectionProxy []>

当使用user.clients ,它返回以下内容:

Client Load (0.1ms)  SELECT "clients".* FROM "clients" WHERE "clients"."user_id" = ? LIMIT ?  [["user_id", 3], ["LIMIT", 11]]
 => #<ActiveRecord::Associations::CollectionProxy []>

我仍在学习 Rails,这是我第一次尝试创建多租户应用程序,所以我可能会遗漏一些东西!

这是我的模型:

// client.rb

class Client < ApplicationRecord
  belongs_to :company
end
// company.rb

class Company < ApplicationRecord
  belongs_to :user
  has_many :clients

  accepts_nested_attributes_for :clients, allow_destroy: true

  scope :sorted, -> { order('position ASC') }
  scope :newest_first, -> { order('created_at ASC') }
  scope :search, ->(query) { where(['name LIKE ?', "%#{query}%"]) }
end
// user.rb

class User < ApplicationRecord
  after_create :create_tenant
  after_destroy :delete_tenant

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, request_keys: [:subdomain]

  validates :email, uniqueness: true

  # Contacts
  has_many :clients, dependent: :destroy
  has_many :companies, dependent: :destroy

  def create_tenant
    Apartment::Tenant.create(subdomain)
  end

  def delete_tenant
    Apartment::Tenant.drop(subdomain)
  end

  def self.find_for_authentication(warden_conditions)
    where(email: warden_conditions[:email], subdomain: warden_conditions[:subdomain]).first
 end
end

这是我的 schema.rb

// schema.rb

ActiveRecord::Schema.define(version: 2019_12_06_151658) do

  create_table "clients", force: :cascade do |t|
    t.string "full_name"
    t.string "email"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
    t.integer "company_id"
    t.index ["company_id"], name: "index_clients_on_company_id"
  end

  create_table "companies", 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 "user_id"
    t.integer "client_id"
    t.index ["client_id"], name: "index_companies_on_client_id"
  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.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "subdomain"
    t.index ["email", "subdomain"], name: "index_users_on_email_and_subdomain", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "companies", "clients"
end

为了从控制台访问租户的数据,您必须先切换到该租户,即

Apartment::Tenant.switch!('tenant_name')
# or 
Apartment::Tenant.switch!(user.subdomain)

要返回默认租户,请键入

Apartment::Tenant.switch # no arguments

有关详细信息,请参阅“切换租户”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM