查询 rails 应用程序中的不同模式数据以进行读写
Query different schemas data in a rails application for read+write
我有两个 rails 应用程序 base1
和 base2
。
base1
使用 db1
并在此数据库中有多个租户,其使用公寓 gem。
base2
是单租户应用程序,主要使用数据库 db2
,还使用来自 db1
的数据。
现在的问题是,base2
应用程序建立了与 db1
的连接,我可以在 [=36] 中获取数据 db1
=] base2
应用程序中的租户。
如何从 base2
应用程序中 db1
的不同模式获取数据?
根据评论中的讨论。
def self.with_schema(schema_name)
class_name = self.name + schema_name.camelize
table_name = self.table_name
if !Object.const_defined?(class_name)
Object.const_set(
class_name, Class.new(self) do
self.table_name = "#{schema_name}.#{table_name}"
end
)
end
class_name.constantize
end
将此添加到您的申请记录中,您可以执行以下操作:Data.schema('schema_name').all
我有两个 rails 应用程序 base1
和 base2
。
base1
使用 db1
并在此数据库中有多个租户,其使用公寓 gem。
base2
是单租户应用程序,主要使用数据库 db2
,还使用来自 db1
的数据。
现在的问题是,base2
应用程序建立了与 db1
的连接,我可以在 [=36] 中获取数据 db1
=] base2
应用程序中的租户。
如何从 base2
应用程序中 db1
的不同模式获取数据?
根据评论中的讨论。
def self.with_schema(schema_name)
class_name = self.name + schema_name.camelize
table_name = self.table_name
if !Object.const_defined?(class_name)
Object.const_set(
class_name, Class.new(self) do
self.table_name = "#{schema_name}.#{table_name}"
end
)
end
class_name.constantize
end
将此添加到您的申请记录中,您可以执行以下操作:Data.schema('schema_name').all