《Rails Recipes》Part II Database Recipes 知识点总结 一

Recipe 17 DRY Up Your Database Configuration
一般设置数据库database.yml:

development:
adapter: mysql
database: DRYUpYourDatabaseConfig_development
username: root
password:
socket: /tmp/mysql.sock

test:
adapter: mysql
database: DRYUpYourDatabaseConfig_test
username: root
password:
socket: /tmp/mysql.sock

production:
adapter: mysql
database: DRYUpYourDatabaseConfig_production
username: root
password:
socket: /tmp/mysql.sock

但是yaml有一个不常用到的功能叫“merge keys”
这个功能允许你把一个hash变量与另外一个hash变量融合在一起
所以上面的代码可以改写为
defaults: &defaults
adapter: mysql
username: root
password: secret
socket: /tmp/mysql.sock
development:
database: DRYUpYourDatabaseConfig_development
<<: *defaults
test:
database: DRYUpYourDatabaseConfig_test
<<: *defaults
production:
database: DRYUpYourDatabaseConfig_production
<<: *defaults




以前见过这种用法能看明白意思,但不知道其中道理,这次明白了

你可能感兴趣的:(mysql,socket,Ruby,UP,Rails)