I have a very strange problem and I don't know where I should look to find it. I am developing a rails 3 app using rspec and factory girl for testing. For some reason, whenever I run any rails commands (eg to rake the db, start the dev server etc) one factory user is created and stored in my development database. The worst part is, it always has the same email, which I am validating the uniqueness of in my app, so the commands won't run until I go in manually delete the record.
I have looked all through my factories file and I don't think I am doing anything strange there, and suggestions where else I might for the code that is doing this?
EDIT: HERE IS MY database.yml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_development
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
# 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: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_test
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_production
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
I figured it out. In my Gemfile, I had:
group :development, :test do
gem 'capybara'
gem "rspec-rails"
gem "guard-rspec"
gem "factory_girl_rails"
...
end
I moved factory girl out of this block onto its own line so it is in the test group only like this:
gem 'factory_girl_rails', :group => :test
No more problems
db/Seeds.rb maybe...but I think that only runs on db:reset and db:seed
Related
I created a new Rails project without database (rails new myApp -O).
How can I add a Postgresql database now ?
Add the pg gem to your gemfile.
# Use postgresql as the database for Active Record
gem 'pg'
Then, if you do not have it, you will need a database.yml file in your config directory so go there and in the config directory create a fole called database.yml which should look like this.
config/database.yml
default: &default
adapter: postgresql
encoding: unicode
username: your username for your Postgresql access
password: your password for your Postgresql access
# 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: The name for your **development** database that you created (probably in PGAdmin?)
test:
<<: *default
database: The name for your **test** database that you created (probably in PGAdmin?)
production:
<<: *default
database: The name for your **production** database that you created (probably in PGAdmin?)
I am trying to connect multiple databases in my rails app one as a primary and the other as a secondary. This is what I have in my database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
# development:
# adapter: postgresql
# database: martin_development
# username:
# password:
# pool: 5
# timeout: 5000
development:
adapter: sqlserver
host:
port: 1433
database:
username:
password:
I left out some data for obvious reasons but the sqlserver is connected but I lost all my data from the postgresql database and I need it as well because it holds separate data for the blog and author logins et cetera. How would I accomplish this?
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
I have created a rails application using postgres database. I am using postgis extension for geo queries. The app is running successfully on my development(local) machine but after deploying my code on heroku server when I run heroku run rake db:migrate it is throwing an error, saying undefined method geometry for ActiveRecord ConnectionAdapters PostgreSQL.
I have geometry datatype in some migrations for storing latitude and longitude.
Note that I have also created PostGIS extension on heroku. And migrations that does not contain geometry datatype executed successfully.
My files are:
Gemfile
ruby "2.3.0"
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
gem 'pg', '~> 0.18'
gem 'rgeo'
gem 'rgeo-activerecord', "~> 5.0.0.beta"
gem "activerecord-postgis-adapter", "~> 4.0.0.beta2"
psql --version is: 9.5.2 on heroku server
psql --version is: 9.4.7 on local server
database.yml
default: &default
adapter: postgis
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: ad_development
production:
<<: *default
database: ad_production
username: ad
password: <%= ENV['DATABASE_PASSWORD'] %>
create_cities migration
def change
create_table :cities do |t|
t.string :name
t.references :state, foreign_key: true
t.geometry :lat_lan
end
heroku run rake db:migrate stops here only.
I am totally confused whether I have used inappropriate gems or I have misconfigured something. Please help!
If using the DATABASE_URL environment variable to set the database connection string, ensure it has the postgis:// (not postgres://) prefix.
ie. postgis://username:password#db_server_url:5432/dbname
url: <%= ENV.fetch('DATABASE_URL', '').sub(/^postgres/, 'postgis') %>
EDIT:
It replaces the url scheme postgres://somewhere.com to postgis://somewhere.com.
It changes to the GIS "protocol", like changing http to https.
You have to replace postgresql with postgis in your database.yml file for the adapter option.
E.g.
default: &default
adapter: postgis
encoding: unicode
pool: <%= ENV["DB_POOL"] || ENV['MAX_THREADS'] || 5 %>
url: <%= ENV['DATABASE_URL'] %>
development:
<<: *default
database: yourapp_development
I currently have a rails project which I deploy to a production server which uses a postgres database. I develop my rails project in Windows, which means that if I want to test locally, I have to change all of the databases in my database.yml file from postgres over to sqlite3 (because setting up Windows to run a postgres server appears to be a pain).
What I would like to be able to do is format my database.yml something like this:
development:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.prod_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
That way I can run rails s -e test locally and test with an sqlite3 database, but when I deploy to my development and production servers I can use postgres.
The problem I am having is that, with the changes to my database.yml shown above, when I run rails s -e test locally I get an error saying that rails could not find the pg gem which seems to imply that it is still trying to use either the development or the production server.
With all the warnings acknowledged, the answer to the question would be to use group in your Gemfile like
gem 'pg', group: [:development, :production]
gem 'sqlite3', group: :test