rails generate not working with mongoid - ruby-on-rails

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.

Related

rake db:create -> stack level too deep, couldn't create database, please check your configuration

I have an old app dating all the way back to Rails 2. We are currently upgrading it.
The database is refusing to create with the settings below. What could be going on?
When I run rake db:create:
Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.
Running cron.rake
Running roles.rake
begin Rakefile
development
stack level too deep
Couldn't create 'project_dev' database. Please check your configuration.
rake aborted!
SystemStackError: stack level too deep
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/instance_variables.rb:15:in `block in instance_values'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/instance_variables.rb:15:in `map'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/instance_variables.rb:15:in `instance_values'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:63:in `as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:180:in `block in as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `each'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:63:in `as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:180:in `block in as_json'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `each'
/Users/ss/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.2.2/lib/active_support/core_ext/object/json.rb:179:in `as_json'
... repeats many times ...
database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= Rails.application.credentials.database[:username] %>
password: <%= Rails.application.credentials.database[:password] %>
development:
<<: *default
database: project_dev
host: localhost
test:
<<: *default
database: project_test
psql postgres:
postgres=# \du
Role name | Attributes | Member of
--------------+------------------------------------------------------------+-----------
applyco | Superuser | {}
postgres=# \list
(List does not include project_dev or project_test).
Remove the gem 'meta_request' to fix the issue.
Source: https://github.com/rails/rails/issues/40781#issuecomment-742807721

Rails and postgresql - no password supplied

I cannot figure out why I am constantly getting this error when running rake db:migrate:status after switching over to Postgresql and Rails 5.2.1
[root:7e2d33988106:~/myapp]# rake db:migrate:status 12:04AM/06.27
rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
/usr/local/rvm/gems/ruby-2.5.1/gems/pg-1.1.4/lib/pg.rb:56:in `initialize'
/usr/local/rvm/gems/ruby-2.5.1/gems/pg-1.1.4/lib/pg.rb:56:in `new'
/usr/local/rvm/gems/ruby-2.5.1/gems/pg-1.1.4/lib/pg.rb:56:in `connect'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `new'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `postgresql_connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:855:in `checkout_new_connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:834:in `try_to_checkout_new_connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:795:in `acquire_connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:523:in `checkout'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1014:in `retrieve_connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/schema_migration.rb:22:in `table_exists?'
/usr/local/rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:124:in `block (3 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.5.1/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:24:in `eval'
/usr/local/rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:migrate:status
(See full trace by running task with --trace)
What I've done so far
I have edited the pg_hba.conf file to show the following:
local all postgres md5
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
and restarted the service. No luck. Same error. The first error I got prior to this was invalid password for postgresql, so i had to change one of the "peer" options to "md5"
If I look into my config/database.yml file, this is what it looks like:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
pool: 5
timeout: 5000
encoding: unicode
username: postgres
database: <%= Rails.application.credentials.dig(:database, Rails.env.to_sym, :database_name) %>
# username: <%= Rails.application.credentials.dig(:database, Rails.env.to_sym, :database_user_name) %>
# password: <%= Rails.application.credentials.dig(:database, Rails.env.to_sym, :database_password) %>
development:
<<: *default
test:
<<: *default
staging:
<<: *default
production:
<<: *default
and when I edit the credentials with rake (using EDITOR=vim rails credentials:edit), this is what this file looks like:
# aws:
# access_key_id: 123
# secret_access_key: 345
# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: <my long key redacted for stackoverflow>
database:
development:
database_name: development
database_user_name: postgres
database_password: <redacted for stackoverflow>
test:
database_name: test
database_user_name: postgres
database_password: <redacted for stackoverflow>
production:
database_name: production
database_user_name: postgres
database_password: <redacted for stackoverflow>
I feel like I've been going hours deep down the rabbit hole and cannot figure out how to get this Rails application working with Postgresql.
I have taken a look at this post here: PG::ConnectionBad fe_sendauth: no password supplied and have tried its suggestion with no luck, and even here How to resolve the error 'fe_sendauth: no password supplied' in Rails using PostgreSQL? with no luck either. I don't have host: '' or host: 'localhost' anywhere in the config/database.yml file (I've tried adding it and still didn't work), so I'm not quite sure this has anything to do with anything here for me.
Any other suggestions?
Add your username and password in production: in database.yml

KeyError: Cannot load `Rails.application.database_configuration`: key not found: "DB_ENV_POSTGRESQL_USER"

So I have been looking everywhere to try and find a solution to this and i have had no luck up to now. I'm ruby on rails so i could very well be missing something basic or be looking in the wrong place for the solution, so i thought i'd just ask. I have a rails API which i forked from my organization and I want to run it locally. I got stuck running this line in the terminal
bundle exec rake create:db
and got this error, I don't really know what to do :/ If this question has already been answered just point me there and i'll go figure it out! Thank you.
Admins-MacBook-Pro:environments nathanshanko$ bundle exec rake db:create
(in /Users/nathanshanko/Desktop/git-repos/voke_api)
rake aborted!
KeyError: Cannot load `Rails.application.database_configuration`:
key not found: "DB_ENV_POSTGRESQL_USER"
(erb):7:in `fetch'
(erb):7:in `'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/application/configuration.rb:104:in `database_configuration'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:41:in `block (3 levels) in '
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `'
KeyError: key not found: "DB_ENV_POSTGRESQL_USER"
(erb):7:in `fetch'
(erb):7:in `'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/railties-4.2.6/lib/rails/application/configuration.rb:104:in `database_configuration'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:41:in `block (3 levels) in '
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/nathanshanko/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `'
Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)
Here is my database.yml file
common: &common
adapter: postgresql
encoding: utf8
reconnect: true
pool: <%= ENV['DB_ENV_POSTGRESQL_POOL'] || 5 %>
database: <%= ENV['DB_ENV_POSTGRESQL_DB'] || 'voke_api' %>
username: <%= ENV.fetch('DB_ENV_POSTGRESQL_USER') %>
password: <%= ENV['DB_ENV_POSTGRESQL_PASS'] %>
host: <%= ENV.fetch('DB_PORT_5432_TCP_ADDR') %>
port: <%= ENV['DB_PORT_5432_TCP_PORT'] %>
development:
<<: *common
production:
<<: *common
staging:
<<: *common
test:
<<: *common
It sounds like you're missing some env vars that define your database credentials. You'll want to set these up (either via the command line, or something like a .env or application.yml file, depending on your project set up).
The error you're getting on DB_ENV_POSTGRESQL_USER is due to the ENV.fetch, which raises an IndexError if the key isn't found. You may also be missing other env vars, but ENV['xyz'] will fail silently without throwing any errors.

Run Specs error: Couldn't load the Unicode tables for

I tried to run my specs but I got the following error:
No DRb server is running. Running in local process instead ...
1) FinancialAccountsQuery parcial_future_releases LIMIT_DATE_FILTER transactions
Failure/Error: let(:financial_account) { FactoryGirl.create(:financial_account_with_church) }
IOError:
Couldn't load the Unicode tables for UTF8Handler (dump format error(0x6b)), ActiveSupport::Multibyte is unusable
# ./spec/queries/financial_accounts_query_spec.rb:4:in `block (2 levels) in <top (required)>'
# ./spec/queries/financial_accounts_query_spec.rb:106:in `create_transaction'
# ./spec/queries/financial_accounts_query_spec.rb:8:in `block (3 levels) in <top (required)>'
I tried to drop all the databases with rake db:drop, then create them again with rake db:create and rake db:migrate, but nothing seems to solve it.
My database.yml
development:
adapter: "mysql2"
encoding: "utf8"
database: "ibc1"
username: "root"
password: ""
host: 127.0.0.1
test:
adapter: "mysql2"
encoding: "utf8"
database: "gi_test"
username: "root"
password: ""
host: 127.0.0.1
Anyone has any idea to solve this ?
Thanks :)

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.

Resources