Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoid yaml combines environment variables for hosts

I am new to rails and mongoid, I have mongoid.yml file that contains the entries as follows:

development:
  # Configure available database clients. (required)
  clients:
    # Defines the default client. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: mycollectionname
      # Provides the hosts the default client can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017

this works properly for development, however, in production, I'd like to specify the host from environment variables like ENV['OPENSHIFT_MONGODB_DB_HOST'] + ":" + ENV['OPENSHIFT_MONGODB_DB_PORT']

I've tried various ways such as this

    hosts:
      - <%= \"#{ENV['OPENSHIFT_MONGODB_HOST']}:#{ENV['OPENSHIFT_MONGODB_PORT']}\" %>

or

    hosts:
      - #{ENV['OPENSHIFT_MONGODB_HOST']:ENV['OPENSHIFT_MONGODB_PORT']}

etc, but none works

like image 762
Zennichimaro Avatar asked Feb 23 '16 08:02

Zennichimaro


1 Answers

In yaml code, <%= %> is meant for you to insert ruby code, you can use Expression Substitution inside it to format your url

Expression substitution is a means of embedding the value of any Ruby expression into a string using #{ and }

Something like this will do:

<%= "#{ENV['OPENSHIFT_MONGODB_HOST']}:#{ENV['OPENSHIFT_MONGODB_PORT']}" %>

In my mongoid project with openshift, I am using its uri: field like this:

uri: <%= "#{ENV['OPENSHIFT_MONGODB_DB_URL']}#{ENV['OPENSHIFT_APP_NAME']}" %>

Please also pay attention to the indentation, it has to be accurate and it has to be space! Tab will cause problems too!

like image 73
Jenny Kim Avatar answered Sep 21 '22 23:09

Jenny Kim