Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
96355e87
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
96355e87
编写于
1月 30, 2016
作者:
J
Jon Moss
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #23366 from maclover7/add-configuation-ar-docs
Add configuration section to "Active Record Basics" guide
上级
a00c36fe
3ded07e7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
102 addition
and
0 deletion
+102
-0
guides/source/active_record_basics.md
guides/source/active_record_basics.md
+102
-0
未找到文件。
guides/source/active_record_basics.md
浏览文件 @
96355e87
...
...
@@ -375,3 +375,105 @@ and to roll it back, `rails db:rollback`.
Note that the above code is database-agnostic: it will run in MySQL,
PostgreSQL, Oracle and others. You can learn more about migrations in the
[
Active Record Migrations guide
](
migrations.html
)
.
Connecting to the Database
----------------------
### `config/database.yml`
When managing connections to the database, the
`config/database.yml`
file is
your best friend. This file helps to keep track of the adapter and
authentication parameters you are using for every database
environment in your application. This file is automatically
generated in all new Rails applications that have Active Record
enabled.
Here's an example of what this file looks like:
```
yaml
default
:
&default
adapter
:
sqlite3
pool
:
5
timeout
:
5000
development
:
<<
:
*default
database
:
db/development.sqlite3
test
:
<<
:
*default
database
:
db/test.sqlite3
production
:
<<
:
*default
database
:
db/production.sqlite3
```
As you can see, there are 3 different database configurations listed
above. One for each of the Rails environments for this application
-- development, test, and production. As well, all three
environments are sharing the same adapter, pool, and timeout
settings, so this was extracted out to the "default" group. This
extraction helps to keep your
`config/database.yml`
file DRY and easy
to read.
A small side note before the next topic -- the test database
configured above will be deleted and restored both before and after
every test run, so make sure you keep your environments' databases
separated and siloed.
### Connecting Manually
In some special cases, you may want to establish connections for you
Active Record models directly inside the model file itself. For this
purpose, the
`establish_connection`
function was created. Say for
example you have a
`Message`
model, like below:
```
ruby
class
Message
<
ActiveRecord
::
Base
end
```
Also, say you want to have this
`Message`
model connect to a
special "msg" database, instead of the one that the rest of the
application is using. In this case, you would do as follows:
Step 1: Add the
`establish_connection`
helper method to your model
file:
```
ruby
class
Message
<
ActiveRecord
::
Base
establish_connection
()
end
```
Step 2: Add the configuration for this new database to your
`config/database.yml`
file, under the
`msg`
(or whichever name you
choose) database name.
Step 3: Turn this database name into a symbol. Remember, using string
keys here is not supported! In this case,
`msg`
converts simply to
`:msg`
. If you are unsure of what the symbolized
database name would be, simply boot up either an
`irb`
or
`rails c`
session, and type in the name of your database as a String, with a
`.to_sym`
at the end, as follows:
```
bash
irb
(
main
)
:001:0>
"msg"
.to_sym
=>
:msg
```
Step 4: The last and final step! Simply use this symbolized
database name as the sole parameter for the
`establish_connection`
method.
```
ruby
class
Message
<
ActiveRecord
::
Base
establish_connection
(
:msg
)
end
```
That's all there is to it! When configuring your database via the
`config/database.yml`
file, or connecting manually in your model
`
,
connecting to your database and making changes is easy when using Active
Record.
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录