# Database.yml YAML questions

**URL:** <https://www.ruby-forum.com/t/database-yml-yaml-questions/74301>\
**Category:** Rails\
**Created:** [November 4, 2006, 7:30am UTC](https://www.ruby-forum.com/t/database-yml-yaml-questions/74301 "2006-11-04T07:30:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [November 4, 2006, 7:30am UTC](https://www.ruby-forum.com/t/database-yml-yaml-questions/74301/1 "2006-11-04T07:30:15Z")

</div>

I’m trying to DRY up my database.yml file.

At first I tried settign some variables in my environment.rb to use in  
the database.yml, but it doesnt seem that the environment.rb variables  
are available to the Erb that gets run on the database.yml (at least not  
during rake).

So now I am trying to use an alias to name my databases, but there is  
really REALLY bad documentation on aliases/anchors in YAML, at lease if  
you google it. And whatever I did figure out doesn’t seem to work.

So I want to replace \*app in the following with the folder name for the  
rails app. Ie. say I did ‘rails newapp’, I would want my databases to  
be named newapp\_dev, newapp\_test, and newapp\_prod. But I dont want to  
have to change it in 3 places in the database.yml. I don;t want to  
change it all in the database yml, but it seems that the alternative  
would be to put a bunch of ENV variables on the commandline (ie. the  
ones in environment.rb dont work).

Here is my database.yml:

login: &login  
adapter: mysql  
username: interact  
password: mediaugen  
host: localhost

development:  
\<\<: \*login  
database: \*app\_dev

test:  
\<\<: \*login  
database: \*app\_test

production:  
\<\<: \*login  
database: \*app\_prod

So I want to have ‘\*app’ replaced with ‘newapp’.

I’ve tried

- &app “fanatics”  
&app “fanatics”  
app: - &app “fanatics”  
app: &app “fanatics”

to set my alias, but none of these seem to work.

What am I doing wrong? Is the an intelligable reference for yaml that  
shows how to use anchors this way?

---

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [November 4, 2006, 7:39am UTC](https://www.ruby-forum.com/t/database-yml-yaml-questions/74301/2 "2006-11-04T07:39:21Z")

</div>

Slain W. wrote:

> I’ve tried
> 
> - &app “fanatics”  
> &app “fanatics”  
> app: - &app “fanatics”  
> app: &app “fanatics”

That should be:

- &app “newapp”  
&app “newapp”  
app: - &app “newapp”  
app: &app “newapp”

as well as

- &app newapp  
-&app newapp  
&app newapp  
app: - &app newapp  
app: &app newapp

---

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [November 7, 2006, 11:02am UTC](https://www.ruby-forum.com/t/database-yml-yaml-questions/74301/3 "2006-11-07T11:02:04Z")

</div>

On Nov 3, 2006, at 10:30 PM, Slain W. wrote:

> So I want to replace \*app in the following with the folder name for  
> the  
> rails app. Ie. say I did ‘rails newapp’, I would want my  
> databases to  
> be named newapp\_dev, newapp\_test, and newapp\_prod. But I dont want to  
> have to change it in 3 places in the database.yml. I don;t want to  
> change it all in the database yml, but it seems that the alternative  
> would be to put a bunch of ENV variables on the commandline (ie. the  
> ones in environment.rb dont work).

> What am I doing wrong? Is the an intelligable reference for yaml that  
> shows how to use anchors this way?

I’m no YAML expert but I think when you use “database: \*app\_prod” it  
will be looking for %app\_prod to be defined somewhere, and isn’t  
going to pick up on &app.

You can however use erb in your YAML like so:

production:  
\<\<: &login  
database: \<%= puts ‘#{app\_name}\_prod’%\>

–  
Craig B.

AIM: kreiggers
