Ruby on Rails: How to work with RDS MySQL instance locally - ruby-on-rails

Environment
Rails version: 6.0.0.rc2
Ruby version: ruby 2.6.3p62 (2019-04-16 revision 67580)
RDS MySql: Ver 8.0.15
Problem
I am trying to connect my RDS MySQL8 instance to my local Ruby on Rails application for testing purposes. My app is working fine with a local version of MySQL8.0.12 with the mysql2 gem.
When I use the RDS DB's details and I run bin/rake db:[anything] VERBOSE=true in the command line, it just hangs without giving ANY information on why it isn't working. Output below.
This is purely to test the db I want to use for a staging environment.
I've connected to the RDS DB from the command line and DBeaver from my local machine, and I've been able to create new tables, so security groups should be ok.
I've used the db connection test script below to ensure that ActiveRecord::Base.connected is true
Connected, initiated, migrated and seeded a local mysql db just in case.
database.yml
#
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: [aws_rds_host_url]
username: <%= ENV.fetch("DB_USERNAME") %>
password: <%= ENV.fetch("DB_PASSWORD") %>
database: db_name
test script found somewhere on stackoverflow (returns "CONNECTED!")
require './config/environment.rb' # Assuming the script is located in the root of the rails app
begin
ActiveRecord::Base.establish_connection # Establishes connection
ActiveRecord::Base.connection # Calls connection object
puts "CONNECTED!" if ActiveRecord::Base.connected?
puts "NOT CONNECTED!" unless ActiveRecord::Base.connected?
rescue StandardError => e
puts e
puts "rescue:: NOT CONNECTED!"
end
Running bin/rake db:create VERBOSE=true || bin/rake db:migrate VERBOSE=true (or anything rake db:) keeps hanging, tried for more than an hour. It does not make any changes to the DB. Stopped with cmd+C it just returns:
bin/rake db:create VERBOSE=true
^CTraceback (most recent call last):
14: from bin/rake:3:in `<main>'
13: from bin/rake:3:in `load'
12: from [home]/[project dir]/bin/spring:16:in `<top (required)>'
11: from [home]/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
10: from [home]/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
9: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
8: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
7: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
6: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
5: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
4: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/client/run.rb:35:in `call'
3: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/client/run.rb:42:in `warm_run'
2: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/client/run.rb:62:in `run'
1: from [home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/client/run.rb:117:in `verify_server_version'
[home]/[project dir]/vendor/bundle/ruby/2.6.0/gems/spring-2.1.0/lib/spring/client/run.rb:117:in `gets': Interrupt

Related

Rails - switching my rails app DB from sqlite3 to postgresql

I've create a RoR application with default database sqlite3. but the problem is sqlite3 is supported on heroku (deploying my app on cloud).
It's been whole day but i can't be able to connect my rails app to postgresql.
This is the steps I've followed till now to connect my app to postgresql
Replace: gem 'sqlite3', '~> 1.4'
with: gem 'pg'
then: bundle install
successfully install all the gems
after that convert my db to postgresql
conflict config/database.yml
Overwrite D:/New folder/amazon-product/config/database.yml? (enter "h" for help) [Ynaqdhm]
force config/database.yml
gsub Gemfile
gsub Gemfile
in that it's overwrite my existing database.yml file to postgresql
after that when i enter command
rake db:create db:migrate
it giving me the error
rails db:create db:migrate
rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'pg'.
Gem Load Error is: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32Backtrace for gem load error is:
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/dll_directory.rb:83:in `add_dll_directory_winapi'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/dll_directory.rb:50:in `initialize'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/singleton.rb:12:in `new'
C:/Ruby30-x64/lib/ruby/site_ruby/3.0.0/ruby_installer/runtime/singleton.rb:12:in `add_dll_directory'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:15:in `block in <main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:26:in `rescue in <main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg.rb:4:in `<main>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:100:in `register'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:55:in `each'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:55:in `block in require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:44:in `each'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler/runtime.rb:44:in `require'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bundler-2.2.33/lib/bundler.rb:175:in `require'
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/rake_module.rb:29:in `load'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/rake_module.rb:29:in `load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:710:in `raw_load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:104:in `block in load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:103:in `load_rakefile'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:82:in `block in run'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/lib/rake/application.rb:80:in `run'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
C:/Ruby30-x64/bin/rake:25:in `load'
C:/Ruby30-x64/bin/rake:25:in `<main>'
Bundler Error Backtrace:
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
Caused by:
RubyInstaller::Runtime::DllDirectory::WinApiError: AddDllDirectory failed for C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/x64-mingw32
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
Caused by:
LoadError: 126: The specified module could not be found. - C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/pg-1.2.3/lib/pg_ext.so
D:/New folder/amazon-product/config/application.rb:7:in `<top (required)>'
D:/New folder/amazon-product/Rakefile:4:in `require_relative'
D:/New folder/amazon-product/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
my database.yml
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: amazon_product_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: amazon_product
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: amazon_product_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass#localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: amazon_product_production
username: amazon_product
password: <%= ENV['AMAZON_PRODUCT_DATABASE_PASSWORD'] %>
I've already installed postgresql also create a new database table called 'details'
hierarchy of pgadmin 4
server --> postgresql 14 --> databases(2) --> postgres, details(which i want to use)

redmine error with puma : error transation do error(NoMethodError (undefined method `do' for false:FalseClass))

I have a problem with redmine(3.4.11) / puma thread
My redmine is connected two databases (10.3.9-MariaDB, azure sql server)
I have created a model who use dynamics365_database.yml for azure
class TIfPmsIssueInfo < ActiveRecord::Base
config_legacy =
YAML.load_file(Rails.root.join('config/dynamics365_database.yml')).symbolize_keys
establish_connection(config_legacy)
end
dynamics365_database.yml (MS azure SQL SERVER):
adapter: sqlserver
encoding: utf8
database: ky*********
username: pm******
password: rh********
host: ky**********
azure: true
pool: 20
timeout: 25000
login_timeout: 60000
I use the following gems:
tiny_tds (2.1.2)
activerecord-sqlserver-adapter (4.2.18)
I use Puma threads on redmine
/usr/local/rvm/gems/ruby-2.3.8/wrappers/puma -e production -p 80 -w 12 –daemon
But sometimes the following errors occur:
redmine plugin
NoMethodError (undefined method `do' for false:FalseClass):
plugins/_crm_requirement/lib/_crm_requirement_hook.rb:168:in `crmmap_save'
plugins/_crm_requirement/lib/_crm_requirement_hook.rb:116:in `controller_issues_edit_after_save'
lib/redmine/hook.rb:61:in `block (2 levels) in call_hook'
lib/redmine/hook.rb:61:in `each'
lib/redmine/hook.rb:61:in `block in call_hook'
lib/redmine/hook.rb:58:in `tap'
lib/redmine/hook.rb:58:in `call_hook'
lib/redmine/hook.rb:91:in `call_hook'
_crm_requirement_hook.rb 168 line:
TIfPmsIssueInfo.transaction do
begin
crm_issue_info = TIfPmsIssueInfo.where("issue_id = #{issue.id}")
...
...
rescue Exception => ex
error = ex.message
raise ActiveRecord::Rollback
end
end
Sometimes it's normal, sometimes an error occurs but I don't know which part of my code is wrong.

change rails environment regarding to request header

I'm pretty new in rails and I would like to ask different database regarding to the origin of the request.
Something like :
http://localhost:80 env: production database: production
http://localhost:8080 env: development database: development
http://localhost:8081 env: test database: test
I already have my three environments defined. I just want to switch !
According to this post, in my application.rb I could adapt
Rails.env = ActiveSupport::StringInquirer.new('production')
to
# application.rb
if request.headers['HTTP_ORIGIN'] == 'http://localhost:80'
Rails.env = ActiveSupport::StringInquirer.new('production')
elsif request.headers['HTTP_ORIGIN'] == 'http://localhost:8080'
Rails.env = ActiveSupport::StringInquirer.new('development')
elsif request.headers['HTTP_ORIGIN'] == 'http://localhost:8081'
Rails.env = ActiveSupport::StringInquirer.new('test')
but it seems I haven't access to method headers :
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing': undefined method `request' for #<App::Application:0x3e9c088> (NoMethodError)
from C:/Sites/KML-backend/config/application.rb:88:in `<class:Application>'
from C:/Sites/KML-backend/config/application.rb:26:in `<module:App>'
from C:/Sites/KML-backend/config/application.rb:25:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I don't really see why you would want to do this, but how about starting three servers instead?
RAILS_ENV="production" rails s -p 80
RAILS_ENV="development" rails s -p 8080
RAILS_ENV="test" rails s -p 8081

Resetting Test Database via rake test is complaining: "database configuration does not specify adapter"

I am attempting to use this bit of useful code from this question:
namespace :db do
namespace :test do
task :reset do
ActiveRecord::Base.establish_connection('test')
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
ActiveRecord::Base.establish_connection(ENV['RAILS_ENV']) #Make sure you don't have side-effects!
end
end
end
Unfortunately I am getting this error:
** Invoke db:test:reset (first_time)
** Execute db:test:reset
rake aborted!
database configuration does not specify adapter
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord- 3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in `resolve_hash_connection'
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:39:in `resolve_string_connection'
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:25:in `spec'
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:127:in `establish_connection'
/home/nick/Projects/yumbin/lib/tasks/test_environment.rake:4:in `block (3 levels) in <top (required)>'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/home/nick/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/nick/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
./bundler_stubs/rake:16:in `load'
./bundler_stubs/rake:16:in `<main>'
Tasks: TOP => db:test:reset
As you can see I am using Ruby 1.9.3 via RVM. I am using Rails 3.2.5, Rake version 0.9.2.2 and Postgresql 9.1, and the pg gem (0.13.2). Additionally, I have no database errors in my environments. Thhe app is deployed and running on Heroku, runs locally in development, and I can run my full test suite without issue. In a Rails console running the line:
ActiveRecord::Base.establish_connection('test')
succeeds. I checked the database.yml in vi for invisible characters but could not find any. Only '$' at the end of each line. I'm starting to think that the answer is incredibly simple and in front of my nose but I just can't see it.
UPDATE: Adding my database.yml file:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: unicode
database: myapp-dev
pool: 5
username: ******
password: *******
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: unicode
database: myapp-test
pool: 5
username: *****
password: ******
production:
adapter: postgresql
encoding: unicode
database: myapp
pool: 5
username: ******
password: *******
You need a test entry in your database.yml file, and I think you're probably lacking one. Make sure you have an entry that looks like this in database.yml:
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
Using whatever adapter and database your development database is using.

rails generate not working with mongoid

I just created an app in rails with mongoid.
When I attempt to follow the tutorial on the mongoid site and run
rails g mongoid:config
I get the following set of errors.
/Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:in `method_missing': undefined method `values' for nil:NilClass (NoMethodError)
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.3.4/lib/mongoid/railtie.rb:64:in `block in <class:Railtie>'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/initializable.rb:30:in `run'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/initializable.rb:54:in `each'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/application.rb:96:in `initialize!'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/XXXXXX/melodizr/config/environment.rb:5:in `<top (required)>'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/application.rb:83:in `require'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/application.rb:83:in `require_environment!'
from /Users/XXXXXX/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands.rb:22:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Please help, this is killing me, I feel like I'm not missing anything but its late and I'm probably wrong.
What version of rails are you running?
Something is messed up. Make sure you have the latest gems (don't specify versions in your gemfile):
Run all of these in your rails app dir:
gem update --system
gem update
bundle install
bundle update
Now that you are up to date try it again. If it stil barfs, just make the file yourself. All that this generator does is make mongoid.yml in the config directory that looks like this:
development:
host: localhost
database: mongoid_test_development
test:
host: localhost
database: mongoid_test_test
# set these environment variables on your prod server
production:
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
# slaves:
# - host: slave1.local
# port: 27018
# - host: slave2.local
# port: 27019
This exact issue is caused when mongoid.yml is missing the environment section. The docs say do this:
host: localhost
database: my_database
But they assume you've used the generator which REALLY does something like this:
development:
host: localhost
database: my_database
That line in the railtie is parsing the yml file assuming you've got an environment set up. Note the other answer that alludes to this and if you followed the instructions WOULD fix it assuming you force overrode the existing broken mongoid.yml
I have observed this issue when you fail to include the three RAILS_ENV sections in your mongoid.yml. I have found that you MUST include all three sections in mongoid.yml YAML config file as below...
development:
host: foo-bar
.....
test:
host: test-bar
....
production:
host: prod-bar
....
I had to remove, temporarily, the gem spring for it to work. Otherwise the task rails g mongoid:config would not finish executing.

Resources