在我的案子里。在我的单个应用程序中,我使用了两个不同的数据库。一个是mysql,另一个是sqlsever。
ActiveRecord::Base.connection.tables
使用此命令,我已经看到mysql数据库表中的所有表。但我不知道使用rails控制台查看特定数据库表的命令。
发布于 2017-11-28 06:12:05
试试下面的代码:
a=ActiveRecord::Base.establish_connection(
:adapter=> "adapter_name",
:encoding=> "utf8",
:reconnect=> "false",
:database=> "db_name",
:pool=> "5",
:username=> "xxxx",
:password=> "xxxxxxx",
:host=>"xx.xx.xx.xx"
)
a.connection.tables发布于 2017-11-28 09:27:28
下面的代码返回database.yml中提到的所有表的数组,特别是数据库连接。
ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["#{other_connection_name}"]).connection.tables其中other_connection_name是database.yml中提到的与DB连接的名称。
例如,database.yml
development:
adapter: mysql2
encoding: utf8
collation: utf8_bin
reconnect: true
database: dev_schema
pool: 5
username: xxx
password: xxxx
host: localhost
otherconnection:
adapter: mysql2
encoding: utf8
collation: utf8_bin
reconnect: true
database: gbl_schema
pool: 5
username: xxx
password: xxx
host: localhost对于上述database.yml,我们使用下面的代码分别检索dev_schema和gbl_schema中的所有表。
ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["development"]).connection.tables
ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["otherconnection"]).connection.tableshttps://stackoverflow.com/questions/47525015
复制相似问题