【发布时间】:2013-08-13 18:01:03
【问题描述】:
我正在开发一个关注this article 的多租户应用程序。问题是当我第一次运行所有迁移时。 schema.rb 文件中只有公共模式的表,但其他模式发生了什么?以及如何创建与public 不同结构的其他架构我不想使用宝石。
请看下面的例子
要为公共架构创建的表
class CreatePerspectives < ActiveRecord::Migration
include MultiSchema
def up
with_in_schemas :only => :public do
# Create table perspectives
end
end
def down
with_in_schemas :only => :public do
drop_table :prespectives
end
end
end
为私有模式创建表
class CreateObjectives < ActiveRecord::Migration
include MultiSchema
def change
with_in_schemas :except => :public do
# Create objectives table
end
end
end
schema.rb
ActiveRecord::Schema.define(version: 20130810172443) do
create_table "perspectives", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
【问题讨论】:
-
我不明白你为什么需要多个模式?您不能只使用一个并根据用户的角色使某些信息无法访问吗?
-
多年来我一直使用并喜欢 Postgres,并且非常熟悉模式及其用法。我也是一名 Rails 开发人员。也就是说,除非必须,否则我永远不会同时使用这两者。你不必。只需设计出你的数据布局(通常我会称之为“模式”,但我不想混淆这个问题)并添加另一个名为“公司”的表或任何拥有记录的表。然后在各种表中赋予它所有权。我有几个这样的应用程序,并不难。
标签: ruby-on-rails-4 multi-tenant rails-migrations rails-postgresql