Rails in a nutshell Глава 3. Active Record | Participants
|
- Statistics
- Participants
- Translate into Russian
- Translation result
- 30% translated in draft.
If you do not want to register an account, you can sign in with OpenID.
Rails in a nutshell Chapter 3. Active Record | ||
Chapter 3. Active Record | ||
Connecting to a Database | ||
Before Active Record can work any of its ORM magic, it needs to know what kind of database it will be working with, and how it's going to connect to that database. In a Rails application, this involves a small configuration file located at config/database.yml. Rails loads the options defined in this file, stores them in a hash accessible as ActiveRecord::Base.configurations, and uses them to establish its database connections. By default, a freshly generated app starts off with a simple database setup that lets you get up and running without any manual configuration. Here's how the untouched database.yml looks: | Перед тем как Active Record сможет выполнять какую либо свою ORM магию, оно должно знать с каким типом баз данных оно будет работать и как ему подключаться к этой базе. В Rails приложении это решается небольшим конфигурационным файлом находящемся в config/database.yml. Rails загружает настройки объявленные в этом файле, располагает их в хэше доступном через ActiveRecord::Base.configurations, и использует их для установления соединения с базой данных. По умолчанию, только что сгенерированное приложение имеет базовые настройки базы данных, которые позволяют запустить приложение без какого либо предварительного конфигурирования. Файл database.yml по умолчанию выглядит так: | |
# SQLite version 3.x | ||
# gem install sqlite3-ruby (not necessary on OS X Leopard) | # gem install sqlite3-ruby (не нужно на OS X Leopard) | |
development: | ||
adapter: sqlite3 | ||
database: db/development.sqlite3 | ||
pool: 5 | ||
timeout: 5000 | ||
# Warning: The database defined as "test" will be erased and | # Предупреждение: База данных определенная как "test" будет стерта и | |
# re-generated from your development database when you run "rake". | # будет пересоздана из базы development когда вы запустите "rake" | |
# Do not set this db to the same as development or production. | # Не указывайте базу одинаковым именем для development или production разработки. | |
test: | ||
adapter: sqlite3 | ||
database: db/test.sqlite3 | ||
pool: 5 | ||
timeout: 5000 | ||
production: | ||
adapter: sqlite3 | ||
database: db/production.sqlite3 | ||
pool: 5 | ||
timeout: 5000 | ||
The adapter properties here are set so that each application environment gets its own SQLite3 database to work with. You will need to have the sqlite3-ruby gem installed before things can work properly. SQLite3 stores databases as single files with locations indicated by the database property of each environment's configuration, and each database is created automatically if it doesn't already exist. The pool property determines the maximum number of database connections in Active Record's connection pool. The timeout property is specific to the SQLite3 adapter and is discussed in ???. | В данном случае свойства адаптера установлены таким образом, что каждая среда разработки получит свою собственную базу данных для работы. Вам будет нужно иметь установленную библиотеку sqlite3-ruby до того, как све зарботает должным образом. SQLite3 сохраняет базу данных как единственный файл в месте указанным в свойстве для каждой конфигурации среды разработки и каждая база создается автоматически, если она еще не существует. Свойство 'pool' определяет максимальное количество соединений к базе в пуле соединений Active Record. Свойство 'timeout' является специальным в описании адаптера SQLite3 и обсуждается в .....??? | |
If you want a new Rails application to be generated with defaults for some other database adapter, you can use the --database option: | Если вы хотите, чтобы новое Rails приложение было сгенерировано с другим адаптером базы данных, вы можете использовать параметр --database: | |
$ rails myapp --database=mysql | ||
This will produce a different database.yml because the MySQL adapter works differently and requires a different set of information to manage its connections. The options for each adapter's configuration are detailed in ???. | Это создаст другой database.yml, потому что адаптер MySQL работает иначе и требует другой набор данных для управления подключениями. Параметры для настройки каждого адаптера, подробно изложены в ???. |
© CC. License: CC
