embedded ruby in database.yml config file

63 views
Skip to first unread message

Stuart Fraser

unread,
Apr 2, 2015, 4:15:00 AM4/2/15
to rubyonra...@googlegroups.com
I have noticed in Rails 4.2 the following in rails console

#database.yml
...
development
:
 
<<: *default
  database
: project_development
  username
: root
  password
: <%= ENV['PASSWORD'] %>

development_replica
:
 
<<: *default
  database
: replicat_db
  username
: replica_user
  password
: <%= ENV['REPLICA_PASSWORD'] %>


If I look at ActiveRecord::Base.configurations['development'] the password set
If I look at ActiveRecord::Base.configurations['development_replicate'] the password is nil

This happens with any embedded ruby that is not in the Rails.env configuration block.

Is this expected behaviour? 

To establish another connection I am supposed to do the following but the embedded ruby isn't evaled
ActiveRecord::Base.establish_connection(:development_replica)

Advice or thoughts?
Thanks

Frederick Cheung

unread,
Apr 2, 2015, 8:44:56 AM4/2/15
to rubyonra...@googlegroups.com


On Thursday, April 2, 2015 at 9:15:00 AM UTC+1, Stuart Fraser wrote:
I have noticed in Rails 4.2 the following in rails console

#database.yml
...
development
:
 
<<: *default
  database
: project_development
  username
: root
  password
: <%= ENV['PASSWORD'] %>

development_replica
:
 
<<: *default
  database
: replicat_db
  username
: replica_user
  password
: <%= ENV['REPLICA_PASSWORD'] %>


If I look at ActiveRecord::Base.configurations['development'] the password set
If I look at ActiveRecord::Base.configurations['development_replicate'] the password is nil

This happens with any embedded ruby that is not in the Rails.env configuration block.
 
Is this expected behaviour? 


The erb step happens before YAML parsing so that sounds unlikely. Are you sure that environment variable is set.

Fred

Stuart Fraser

unread,
Apr 8, 2015, 10:43:33 PM4/8/15
to rubyonra...@googlegroups.com
I've decided to use dotenv-rails and this seems to have remedied things. Perhaps it was an eval order issue.

I did have ENV populated with all values I expected to see, they just weren't in the configurations.

Thanks

Stuart Fraser

unread,
Apr 19, 2015, 9:31:58 PM4/19/15
to rubyonra...@googlegroups.com
So I worked out that Spring was my problem. If the ENV['DATABASE_URL'] or any other setting is not set then Spring still starts and keeps running with things unset.

I lost far too much time wondering why I could see my ENV variables in Rails Console but database.yml wasn't getting set.

spring stop
Reply all
Reply to author
Forward
0 new messages