ymRuby on rails: Running specs for models - ruby-on-rails

I'm new to rspec framework, and I have a rails Application that I wish to use Rspec to write unit tests for it.
I followed the instructions in https://github.com/rspec/rspec-rails to install the rspec-rails gem.
I've written a very simple spec file that requires 'rails_helper' and have some pending tests only.
When I try to run this file using command bundle exec rspec I get this error
An error occurred while loading ./spec/models/invoice_spec.rb.
Failure/Error: include DeviseTokenAuth::Concerns::User
Mysql2::Error:
Access denied for user 'root'#'localhost' (using password: NO)
How do I resolve this so that I can run my specs?
Edit : database.yml file
default: &default
adapter: mysql2
encoding: utf8
username: <%= Rails.application.secrets[:mysql_db_user] %>
password: <%= Rails.application.secrets[:mysql_db_password] %>
pool: 5
timeout: 5000
development:
<<: *default
database: tomav_dev
test:
<<: *default
database: tomav_test

Related

Unable to create Database in Rails using rake db:create

I created a new rails project and did few configuration changes in database.yml.
Then had to create the database using the command rake db:create inorder to continue developing the application but I get the error below.
Els-MacBook-Pro:eshop el$ rake db:create
warning ../../package.json: No license field
FATAL: role "eshop" does not exist
Couldn't create 'eshop_development' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: role "eshop" does not exist
Tasks: TOP => db:create
(See full trace by running task with --trace)
Els-MacBook-Pro:eshop el$
I will also post the contents of my database.yml below:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
host: localhost
database: eshop_development
username: eshop
password: eshop
test:
<<: *default
host: localhost
database: eshop_test
username: eshop
password: eshop
production:
<<: *default
database: eshop_production
username: eshop
password: <%= ENV['ESHOP_DATABASE_PASSWORD'] %>
I will be grateful if someone can help me figure out why I cannot create a db. I have read all types of solutions and none seems to be working. Thanks.
In your database.yaml you need to add a port: 5432, and then Try rails db:create, as rake has been deprecated.
create a role in your postrgesql database named 'eshop'

receiving "PG::ConnectionBad fe_sendauth: no password supplied" error when storing passwords in ENV variables in rails

I recently switched db's from sqlite3 to PG. My username and password for pg were hard-coded, and although it was working, this was not safe practice. SO I stored my password in an ENV variable in a .yml file, and I reference that variable in my database.yml file, but when I run the server, it gives me the error "PG::ConnectionBad
fe_sendauth: no password supplied"
the following is my pg_keys.yml file
PG_PASSWORD: "***********"
And the following is my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: stockapp_development
username: postgres
password: <%= ENV['PG_PASSWORD'] %>
host: localhost
# 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: stockapp_test
host: localhost
username: postgres
password: <%= ENV['PG_PASSWORD'] %>
production:
<<: *default
database: stockapp_production
username: stockapp
password: <%= ENV['STOCKAPP_DATABASE_PASSWORD'] %>
Why is it saying that no password is supplied? Does my database.yml not see the password in the other file? Do I need to export the password?
It is because ENV variables come from https://github.com/bkeepers/dotenv.
Install this gem by adding it to your Gemfile, then run bundle and set the variable PG_PASSWORD=*********** in .env in your root. Then it should work fine.

ActiveRecord task executing in wrong environment with Whenever

I have to run a Cron task involving some data cleaning in my ActiveRecord database. I am using Whenever gem. Here is the code :
schedule.rb
every 1.hour do
rake 'notifications:clear'
end
notifications.rake
namespace :notifications do
task clear: :environment do
Rpush::Notification.delete_all
end
end
Running this gives me the following error:
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "user_prod" does not exist
I am in development environment. Here is my database.yml file :
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: development_database
test:
<<: *default
database: test_database
staging:
<<: *default
database: staging_database
username: user_staging
password: <%= ENV['DATABASE_PASSWORD'] %>
production:
<<: *default
database: production_database
username: user_prod
password: <%= ENV['DATABASE_PASSWORD'] %>
Any ideas on why my ActiveRecord instruction seems to connect to my production environment ? Thanks in advance !
Update Your code with below :-
In schedule.rb file :-
every 1.hour do
rake 'notifications:clear', :environment => "development"
end
And finally executed this command :-
whenever --update-crontab
OR
Clear existing cron jobs.
crontab -r
Update cronjob with the environment.
whenever --update-crontab --set environment='development'

Mysql Connection adapter error

I tried running db:migrate on my app and I'm getting this error.Not sure what is the cause.
My database is MySQL
using MySQL 64-bit connector
ruby version:ruby 2.2.6p396 (2016-11-15 revision 56800) [i386-mingw32]
I've done a google search and I'm not getting anything..Can anyone explain this error pls?
NotImplementedError: NotImplementedError
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:85:in exec_query'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:377:in 'select_prepared'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:39:inselect_all'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:95:in select_all'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/querying.rb:39:infind_by_sql'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/relation.rb:702:in `exec_queries'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rake-12.0.0/exe/rake:27:in <top (required)>'
C:/RailsInstaller/Ruby2.2.0/bin/rake:23:inload'
C:/RailsInstaller/Ruby2.2.0/bin/rake:23:in `'
Tasks: TOP => db:
migrate
You can try this..
Check your config/database.yml file.
your config/database.yml must look like and username, password should be change your mysql's username, password..
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: username
password: password
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: uBuild-rails_development
test:
<<: *default
database: uBuild-rails_test
production:
<<: *default
database: uBuild-rails_development
username: username
password: password
Also check you Gemfile
gem 'mysql2', '< 0.3' # as stated above
I had the same problem, what I did was to create the project again using this command:
rails new my_project -d=mysql
This way the configuration in database.yml and gemfile is created automatically avoiding problems. You only need to edit database.yml mysql password after this you can use
rails g scaffold Examples attrib1:string attrib2:string
to create views etc
rake db:create
to create the database and then
rake db:migrate
hope this helps...
Database file is in config/database.yml and configuration be
default: &default
adapter: mysql2 #if use postgres the add postgresql
pool: 5
timeout: 5000
username : username #mysql username
password : password #mysql password
development:
<<: *default
database: application_name
test:
<<: *default
database: application_name
production:
<<: *default
database: application_name
Run following command to setup database:
run rake db:create
run rake db:migrate

Rails_admin dont work in production

I got some problems with my rails_admin gem in the production when I trying make first user admin in Rails console.
In the development all works fine. Look at the error and code.
Error terminal:
2.3.0 :001 > u = User.first
ActiveRecord::NoDatabaseError: FATAL: database "myApp_development" does not exist
database.production.yml
default: &default
adapter: postgresql
encoding: UTF-8
pool: 5
development:
<<: *default
database: myApp_development
username: deployer
password: password
test:
<<: *default
database: myApp_test
username: deployer
password: password
production:
<<: *default
database: myApp_production
username: deployer
password: password
To run rails console in the production environment you should use bundle exec rails console production or bundle exec rails console RAILS_ENV=production, the command bundle exec rails console run console in the development env by default.
Create the database table and do
rake db:migrate RAILS_ENV=production
rails console RAILS_ENV=production

Resources