[英]Updating a record through the rails console when using the Apartment gem
[英]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.