Rails Engine + Mongoid: No configuration could be found for a session named 'default' - rails-engines

I've created a Rails Mountable App and added 'mongoid' and 'rspec' gem's. If I try to run my specs now I get the following error:
Mongoid::Errors::NoSessionConfig:
Problem:
No configuration could be found for a session named 'default'.
Summary:
When attempting to create the new session, Mongoid could not find a session configuration for the name: 'default'. This is necessary in order to know the host, port, and options needed to connect.
Resolution:
Double check your mongoid.yml to make sure under the sessions key that a configuration exists for 'default'. If you have set the configuration programatically, ensure that 'default' exists in the configuration hash.
When I add the Mongoid.load!(Rails.root.join("config", "mongoid.yml")) line to spec_helper.rb everything works normal.
Why is that and how can I get the functionality like in a normal Rails app where I don't need to call the load function?
mongoid.yml
development:
sessions:
default:
database: dummy_development
hosts:
- localhost:27017
options:
options:
test:
sessions:
default:
database: dummy_test
hosts:
- localhost:27017
options:
consistency: :strong
max_retries: 1
retry_interval: 0
Versions:
gem 'rails', '~> 3.2.12'
gem 'mongoid', '~> 3.1'
gem 'rspec-rails', '~> 2.13'

you probably missed require 'rails/mongoid' in your spec_helper.rb file.
Had someone having the same issue in here https://github.com/mongoid/mongoid/issues/2894#issuecomment-14903927
Try adding that require, that should fix it.

This worked for me on my machine
1: Add this to your config/application.rb
Mongoid.load!("path to your mongoid.yml")
2: And change your mongoid.yml from (Only for mongoid version < 5):
This
development:
clients:
default:
database: database_for_development
hosts:
- localhost:27017
test:
clients:
default:
database: database_for_test
hosts:
- localhost:27017
production:
clients:
default:
database: database_for_production
hosts:
- localhost:27017
To:
development:
sessions:
default:
database: database_for_development
hosts:
- localhost:27017
test:
sessions:
default:
database: database_for_test
hosts:
- localhost:27017
production:
sessions:
default:
database: database_for_production
hosts:
- localhost:27017

This is probably because of two simultaneous conditions: ( there is no production section in mongoid.yml ) AND ( Heroku treats Rails applications as production by default ).
Fixing either one shall suffice.
1. There is no production section in mongoid.yml
Add production section to mongoid.yml, as explained at Heroku , e.g.
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: true
2. Heroku treats rails applications as production by default
Set Heroku environment to development, or a add a new environment which would be specific to Heroku, as explained at Heroku, e.g.
heroku config:set RACK_ENV=development RAILS_ENV=development --remote development

And restart the server after making changes to mongoid.yml

I've found this working - notice there is no "sessions", only "clients"
production:
clients:
default:
uri: <%= ENV['MONGODB_URI'] %>
options:
skip_version_check: true
safe: true

Try to regenerate a new mongoid.yml:
rails g mongoid:config
And after change mongoid.yml with your values.

Related

rake db:create - rake aborted

Hi im trying to follow this link to create a scraper and I am stuck trying to create a db
https://towardsdatascience.com/job-board-scraping-with-rails-872c432ed2c8
I get this error when typing rake db:create I've tried creating inside and outside the folder and I get this error I am not sure why, thank you very much. I have been closely following the link so I have all the same files and am sure everything was installed properly.
rake aborted!
ActiveRecord::DatabaseConfigurations::InvalidConfigurationError:
'{ default => }' is not a valid configuration. Expected '' to be a URL string or a Hash.
This is my database.yml file
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: scraper_development
test:
<<: *default
database: scraper_test
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
# 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.
Thanks in advance everyone!
Update
Thanks for the pointer I had [the database.yml file] indented but I though an error told me to remove tabs. new issue now:
could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Couldn't create 'scraper_development' database.
Please check your configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"
The file is in YAML format. The sub-items must be indented.
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: scraper_development
You can add " gem 'pg', '>= 0.18', '< 2.0' " to the gem file and make the necessary settings in the YAML file.
I ran into the same problem and what I did was to fix the indentation, and then it worked.
The database.yml file is in YAML format. so make sure the sub-items are indented correctly. Please check the example below on how you are supposed to indent it.
This is how your database.yml file should look like
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: scraper_development
test:
<<: *default
database: scraper_test
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>

Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' on heroku push

When i do git push heroku master ,it fails at rake assets precompile and i get an error Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' heroku
What i already know
Since i am using Rails 4,Definitely config.assets.initialize_on_precompile = false is out of the picture because is no longer required.
heroku labs:enable user-env-compile this feature is no longer supported by Heroku.
After doing research online, I learned that the solution might be here
heroku build pack
But i don't understand how to run those commands.
I keep getting bash: bin/compile: No such file or directory
How do i resolve this issue? I am using Rails 4.2.5 ,Ruby 2.3.0 and the Db is Mysql locally.Thank you in advance.
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: password
host: localhost
development:
<<: *default
database: respect_development
test:
<<: *default
database: respect_test
production:
<<: *default
database: respect_production
username: respect
password: <%= ENV['RESPECT_DATABASE_PASSWORD'] %>
It sounds like your database.yml is configured to use a local database (127.0.0.1) even on Heroku. What is in your database.yml? I would remove that file from git and check your Heroku config vars for DATABASE_URL. You should see a reference to the database on Heroku, not 127.0.0.1.
I just had to precompile assets rake assets:precompile locally and then push it git push heroku master
and in my environments/production.rb
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
Also i added this to my Gemfile
group :production do
gem 'rails_12factor'
end

Correctly configuring a Postgres DB for a Rails 4.2 app on Heroku

I'm confused about how to configure the DB in a Rails 4.2 app that uses Postgres and Heroku.
Following the advice in this Heroku guide, you'll get a config/database.yml like this:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
production:
<<: *default
database: app_name_production
But when I tried this, my development and test environment were using the same DB as the staging environment (note the file has no configuration for staging). That's no good.
This Heroku guide for connecting to the DB in Ruby mentions that any Rails apps before 4.2 would have their database.yml file overwritten by Heroku. Heroku will parse the DATABASE_URL environment variable and create a new database.yml file.
So I guess it's possible to just leave out the configuration in database.yml for any environments that you have on Heroku, such as staging and production. Your database.yml file could essentially look like Hound's (note the lack of a production configuration).
development: &default
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
test:
<<: *default
database: app_test
But since we're using Rails 4.2, I don't think Heroku will override the database.yml file. In that case, do you have to specify DB configuration in database.yml for our environments on Heroku? Or is it still safe to leave them out? If we do need to specify the configuration for Heroku environments, will the following be sufficient?
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
I'm also confused about the proper configuration for the development and test environments. As I mentioned above, the first configuration shown has those environments using the staging DB on Heroku, instead of a local DB.
This Heroku guide says to export the DATABASE_URL environment variable for your app to connect (once Postgres is installed and you can connect to it).
Assuming you export the DATABASE_URL env var as specified in the article, what does your configuration for development and test have to look like? Do we go with the configuration as shown in the first guide, e.g.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
Or do we use a configuration as shown in this Heroku guide (which uses host and username)
development:
adapter: postgresql
host: localhost
username: user
database: app-dev
Update 1: Here's what I now know. A staging and production config isn't necessary in config/database.yml if you deploy to Heroku, no matter your Rails version. Prior to 4.2, Heroku would generate it's own database.yml file based on the value of the DATABASE_URL environment variable, overwriting your config file (if it exists). Since Rails 4.2, your app will use the DATABASE_URL environment variable directly (bypassing the database.yml file), so Heroku doesn't need to (and won't) generate a config file.
I also figured out why my development and test environments were using the remote staging DB from the Heroku app instead of the local DBs specified in their database.yml config. It was because my local .env file for development is based off of my staging .env file which contains environment variables for connecting to the database such as DATABASE_URL. Because DATABASE_URL was present in my development .env file, my Rails 4.2 app was using it and thus connecting to the staging DB. To fix it, I removed those environment variables from the development .env file and created the local DBs (and ran the migrations) with bundle exec rake db:setup.
Update 2: This section of the Rails Guides goes into more detail about how to configure the DB, worth a read: http://guides.rubyonrails.org/configuring.html#configuring-a-database
Most of your assumptions are correct. The following is a reasonable database.yml configuration file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
Make sure that the RAILS_ENV is properly set on Heroku (either staging or production), or Rails will default to development.
Locally, the test will pick the test environment. By default, the app will start in development mode, using the development environment.
In fact, many developers choose to ignore the database.yml in version control and never publish it into the repo. The reason for that is, that databases may be different on different machines, that's a reason not to keep the configuration common.
I'm working on a Rails 4.2 project right now, and Heroku has no problem with having no database.yml at all (both with PostgreSQL and MySQL, we tested both). Why? Because DATABASE_URL provides all the information necessary to access the database, even adapter name. How? Here's the formula:
adapter://username:password#hostname:port/database?options
Locally, I'm using Postgres with peer authentication: the database server assumes the same username that I'm using in my OS, username is covered, password is useless. Local machine is assumed when no host is given, although I can't tell if it tries to communicate via TCP/IP or Unix domain sockets, so I'm fine without host.
So the configuration you refer to as "shown in the first guide" is reasonable: it contains a minimum amount of settings and allows you to create more environments quite easily.
Heroku doesn't create a database.yml on rails 4.2 because as of that version rails will detect the presence of that environment variable and use it to configure the database connection.
Adding
production:
url: <%= ENV['DATABASE_URL'] %>
Makes it a little more obvious what is happening for those who might not be aware of it but won't change the behaviour. The configuring rails guide has more info in interactions between database.yml and DATABASE_URL if you need it.
To connect with ActiveRecord without Rails (e.g. sinatra):
url = URI.parse(ENV['DATABASE_URL'])
ActiveRecord::Base.establish_connection(
encoding: 'unicode',
pool: 5,
timeout: 5000,
reconnect: true,
adapter: url.scheme,
host: url.host,
database: url.path.sub(%r{^/}, ''),
username: url.user,
password: url.password
)

Rails database name dynamically by user choice in Mongodb

While installing my app , I want to ask question like
Name of database?
and user will input name they want for database.
Database name is inside config/mongoid.yml
development:
sessions:
default:
database: project_development
hosts:
- localhost:27017
options:
I want to make the database like project_development dynamically by user input while performing bundle install or before bundle install for app.Is there any steps to make this happen?
If config/mongoid.yml is passed through ERB like config/database.yml is, then you can do the following trick:
development:
sessions:
default:
database: <%= ENV['MONGO_DB_NAME'] || 'default_db_name' %>
hosts:
- localhost:27017
options:
Then start rails with MONGO_DB_NAME=some_name rails s. If you omit MONGO_DB_NAME from startup, it will fallback to default_db_name.

"rails server" using the wrong database

I recently switched a relatively new rails app from sqlite3 to Amazon RDS and configured my database.yml file to use the RDS database in the production environment only.
But now, whenever I try to do any local action on my database (e.g. rails server, rails console, rake db:migrate, etc.) it does that action to the production DB on Amazon's servers rather than my local sqlite3 DB, which is my development DB.
# database.yml
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql2
host: mydb.mydbhost.us-east-1.rds.amazonaws.com
reconnect: false
database: mydb
username: myusername
password: mypassword
What am I doing wrong?
UPDATE: Here's my environment.rb file:
# environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Heroku environment variables for local use
heroku_env = File.join(Rails.root, 'config', 'heroku_env.rb')
load(heroku_env) if File.exists?(heroku_env)
# Initialize the rails application
Myapp::Application.initialize!
your not using productions settings
try
rails s -e production
or
RAILS_ENV=production rails s
RAILS_ENV=production rake db:migrate
Figured out the problem after taking a day to get away from it. It's a silly mistake on my part, but I thought I'd post the solution in case someone else encounters a similar issue.
As you can see from my environment.rb file above, I have a heroku_env.rb file, which contains all of my heroku-specific environment variables on my local machine for development purposes. In that file, I declared a ENV['DATABASE_URL'] variable, which links to my Amazon RDS database. Deleting this from the file solved the problem!
Thanks to everyone who offered answers to help!

Resources