[英]How to connect to an existing database in Ruby on Rails
I have a table named User at my project and I want to access another database of an another existing project.我的项目中有一个名为User的表,我想访问另一个现有项目的另一个数据库。 The database that I want to access it makes part of a project that I need to conceed access to my table User.
我要访问它的数据库是我需要授予对我的表 User 的访问权限的项目的一部分。 The reason is that my table User that is responsable to authenticate an user base, and after their authentication they will return a token and so they can access the another database.
原因是我的表 User 负责对用户群进行身份验证,并且在进行身份验证后,他们将返回一个令牌,因此他们可以访问另一个数据库。 The database.yml of my table User is configured like this:
我的表 User的 database.yml配置如下:
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
The database of the another project that I want to access is configured as follows:我要访问的另一个项目的数据库配置如下:
default: &default
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_bin
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
variables:
sql_mode: TRADITIONAL
development:
<<: *default
username: root
password:
database: <company_name>dev
test:
<<: *default
database: <company_name>_test
username: root
password:
production:
<<: *default
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
How can I solve this problem?我怎么解决这个问题?
You can add another database to the first config (adjust as needed, I just copied your config from the second):您可以将另一个数据库添加到第一个配置中(根据需要进行调整,我只是从第二个配置中复制了您的配置):
...
other:
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_bin
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
variables:
sql_mode: TRADITIONAL
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
You can then create a subfolder in your app/models
directory to give you ActiveRecord
access to the other database.然后你可以在你的
app/models
目录中创建一个子文件夹,让你ActiveRecord
访问另一个数据库。 First create a base class:首先创建一个基础 class:
# app/models/other/base.rb
module Other
class Base < ActiveRecord::Base
establish_connection configurations['other']
self.abstract_class = true
end
end
Then add a User model within that module:然后在该模块中添加用户 model :
# app/models/other/user.rb
module Other
class User < Base
self.table_name = 'users'
self.primary_key = 'id'
end
end
Now, you can refer to the "other" User table like this:现在,您可以像这样引用“其他”用户表:
Other::User.find(other_user_id)
Also keep in mind you will need to install the mysql2
database adapter in your first project's Gemfile.还要记住,您需要在第一个项目的 Gemfile 中安装
mysql2
数据库适配器。
Also note that you'll probably want a better name/module than "Other", I just used that as the example here.另请注意,您可能需要一个比“其他”更好的名称/模块,我只是在这里举个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.