Configuring a Project for a Database Ruby on Rails - mysql

So I'm new to programing and I'm teaching myself Ruby on Rails to get started. I have mysql running and I've downloaded Ruby ruby 2.3.0 and Rails 3.2.22.2 because these are the versions being used in the video tutorial.
I was generating a controller & view, however when it came to the moment of truth to see if everything worked there were problems. I programmed index.html.erb to say "hello world" in my browser but received this message:
ActiveRecord::ConnectionNotEstablished
I believe this is because it's trying to contact the database. So I started to create the database on mysml, and as I reach the point where I create a schema.rb in the dp folder. Only when I type the command in terminal I get this:
julian$ rake db:schema:dump
rake aborted!
LoadError: Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (can't activate mysql2 (~> 0.3.10), already activated mysql2-0.4.4. Make sure all dependencies are added to Gemfile.)
/Users/julian/Sites/simple_cms/config/environment.rb:5:in `<top (required)>'
Gem::LoadError: can't activate mysql2 (~> 0.3.10), already activated mysql2-0.4.4. Make sure all dependencies are added to Gemfile.
/Users/julian/Sites/simple_cms/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:schema:dump => environment
(See full trace by running task with --trace)
What do I do? I have been stuck on this for hours, does anyone have some guidance?

Please add gem 'activerecord-mysql2-adapter' to your Gemfile

i think you can uninstall the wrong one
gem uninstall <gem_name>
and then install the correct one
gem install <gem_name>

Related

`require': cannot load such file -- bundler (LoadError)

I was following up from https://jekyllrb.com/docs/installation/ to install Jekyll and I have made sure that I have Xcode command line tools, gcc and make installed. Version of ruby is 2.1.1 I have installed Jekyll using
gem install jekyll
I got following success message
Done installing documentation for safe_yaml, rouge, forwardable-extended, pathutil, mercenary, liquid, kramdown, ffi, rb-inotify, rb-fsevent, listen, jekyll-watch, sass-listen, sass, jekyll-sass-converter, colorator, public_suffix, addressable, jekyll after 14 seconds
19 gems installed
But when I execute jekyll --version
I got following error
/Users/amar/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bundler (LoadError)
from /Users/amar/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/amar/.rvm/gems/ruby-2.1.0/gems/jekyll-3.5.2/lib/jekyll/plugin_manager.rb:46:in `require_from_bundler'
from /Users/amar/.rvm/gems/ruby-2.1.0/gems/jekyll-3.5.2/exe/jekyll:9:in `<top (required)>'
from /Users/amar/.rvm/gems/ruby-2.1.0/bin/jekyll:23:in `load'
from /Users/amar/.rvm/gems/ruby-2.1.0/bin/jekyll:23:in `<main>'
from /Users/amar/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/amar/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `<main>
Error was solved by installing new version of bundler again, I had installed it previously as well but there was version issue
gem install bundler
Check your bundler version by
bundler --version
If everything is fine it will show version #
Then again run jekyll --version and everything should go fine

Rails Setup: mysql2 gem always causing problems

I'm fairly new when it comes to commandline or Rails, but work on a rails project where I'm doing the frontend.
I've installed all gems that are needed for the project, but lately some errors occur when doing a rake:db migrate or today when trying to deploy the current version via cap production deploy
The problem I'm facing seems to have todo with the mysql2 gem. At least that is what the console says.
When running cap production deploy I get this error:
rake stdout: Nothing written
rake stderr: rake aborted!
Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile.
and further down this …
Gem::LoadError: can't activate mysql2 (~> 0.3.10), already activated mysql2-0.4.2. Make sure all dependencies are added to Gemfile.
What do I have todo here? When I do gem list I see that mysql2 is installed.
Thank you,
Matt
Rails used to have the mysql2 gem locked down to ~>0.3 in the codebase.
So that is what it looks for but your Gemfile will simply state
gem "mysql2"
Pretty sure they've updated this in the latest release of rails 4 so either:
Update to the latest version of rails 4 or use a specific version of the mysql2 gem
gem "mysql2", "~> 0.3.20"

cannot load such file -- mysql/mysql_api

I am completing the tutorial on Ruby on Rails 3 Essential Training through Lynda.com on Windows 7, and I am in the section on "Databases and Migrations". I am attempting to sync the database I have created in MySQL with my Rails project, however I have been running into an abort message. The instructor asked my class to go through Command Prompt to open up our rails project and perform a Rake to build a "schema.rb" with this code:
rake db:schema:dump
Yet each time I type the code in I have gotten this error message:
C:\Users\User\Documents\simple_cms>rake db:schema:dump
rake aborted!
cannot load such file -- mysql/mysql_api
C:/Users/User/Documents/simple_cms/config/application.rb:7:in `<top (required)>'
C:/Users/User/Documents/simple_cms/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
Some solutions I have attempted:
I have installed the binary version of mysql. It was built using MySQL Connector/C version 6.0.2. and I added
libmySQL.dll
to
C:\RailsInstaller\Ruby1.9.3\bin
from dev.mysql.com
I have attempted to download the "mysql2" Gem through Command Prompt with the following result:
C:\Users\User\Documents\simple_cms>gem install mysql2
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while..
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
Help?
Or just instalo mysql2 gem. It Works for me at Wim 10.
Are you trying to access a 64 bit MySQL installation from 32 bit Ruby installation. This seems to be problem with a lot of ppl, even I encountered this when I had a new Win7(64 bit) new machine.
try the solution provided on this blog :-
http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/

Rails application throughs MySql GEM error

I am trying to run the "Redmine" project management application written in Rails.
https://github.com/redmine/redmine
I downloaded it from Github and now while trying to run it (by double clicking on the rails file, I get this error in terminal:
Could not find gem 'mysql (~> 2.8.1) ruby' in the gems available on this machine
I have mysql 2.9.1 already installed.
I am new to the whole Rails thingy. I just installed it to be able to run this application.
So any advice on helping me run this app would be greatly appreciated.
Thanks
could it be that it doesnt work with the latest mysql version? make sure you stick this in your Gemfile in Rails app.
gem 'mysql', '~> 2.8.1'
also make sure its under test, or development if you want to run that locally.
group :test, :development do
gem 'mysql', '~> 2.8.1'
end
Probably easiest will be to tell Redmine to use your version of mysql:
Go to
\YourRedMineInstallFolder\apps\redmine\htdocs
open the Gemfile & Gemfile.lock and change/add this line at end or in mysql area.
gem "mysql", "~> 2.9.1"
Then go to the services and restart all redmine services specially ones with "..thin" in their name(This is pretty important).

Rails and MySQL - ActiveRecord::ConnectionNotEstablished in production environment

I'll try to be short:
Using Rails 3.2.1 and mysql2 for databases; running on Ubuntu 11.10
In development environment everything works just fine
When I start my server (WEBrick) in production, it boots up fine, but after loading any page, it raises the following error:
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):
activerecord (3.2.1)
lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in
retrieve_connection'
activerecord (3.2.1)
lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in
`retrieve_connection'
activerecord (3.2.1)
lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in
`connection'
...
/home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in
`service'
/home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/metjush/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block
in start_thread'
After trying to find any possible source of error, and consulting like a billion different websites and similar reports, I am still not able to find what's wrong. My database.yml (running on localhost right now, and have my settings in production environment the same as for development, which works) is fine, my Gemfile is alright (gem 'mysql2' included, don't worry), the production database is created and rake db:migrate had no problems migrating it... Any other possible source of error I did not think of or did not find on the internet?
I stress, this only happens in production
Thanks for your help
EDIT Posting my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.1'
gem 'mysql2'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'rufus-scheduler'
gem 'gmaps4rails'
Hope this helps - although I don't really see anything wrong with it.
EDIT TWO
Oddly enough, I have no problems when I start the rails console in the production environment - MySQL queries work, model instances can be saved without problems. I only get the aforementioned error when I try to access the database from the browser.
For me it came down to a misconfigured database.yml file. To confirm the correct formatting you can do the following from the directory where the .yml file is stored:
irb
require 'yaml'
a = YAML::load(File.open("database.yml"))
if you have an error you'll see something like this in response...
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> a = YAML::load(File.open("database.yml"))
Psych::SyntaxError: (<unknown>): could not find expected ':' while scanning a simple key at line 17 column 3
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in `parse'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in `parse_stream'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:151:in `parse'
from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:in `load'
from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
In my case, I forgot to leave a space after the ":" when entering the database password.
i had the same issue. Spree latest version is still not compatible with rails 3.2.0/3.2.1
so use the rails 3.1.3 and the latest spree version
In my case, rails version was 3.2.0 and spree default version was 0.40.0. i changed spree version to 1.0.0.rc3 and that shown me rails compatibility issue, so i changed rails version to 3.1.3 and all went fine.
You can use the latest spree stable version i guess its 0.70.3.
you have to create a mysql database first. and then go to your application directory and edit database.yml file (~/config/database.yml) with your database information.
like:
development:
adapter: msql2
database: your database path
username: mysql username
password: mysql password
pool: 5
timeout: 5000
I encountered a similar problem in a Rails engine gem, and eventually tracked it down to a scope declaration (eg: scope :owing, where...) on one of the classes. Commenting that out resolved the ActiveRecord::ConnectionNotEstablished but resulted in a failing spec (obviously).
The engine is using the Combustion gem to include a minified rails app for specs, and the problem occurred regardless of db type. Hope that helps in some way.
Edit: Here's a rails issue for it.
You've probably already moved on with this but in case someone else gets to this page, here are a couple of things to try.
Are you sure there are no typos when you try to launch the server in production mode?
bundle exec rails s --environment=production
If there is a typo in "production" rails will not complain when it tries to start the server but it will try to look for that typo in the database.yml file and since it does not exist you will get the error you mentioned
Another thing to try: Check whether you database.yml file is formatted correctly. YAML is really picky with tabs and spaces so double check by doing the following in your console
irb
require 'yaml'
a = YAML::load(File.open("config/database.yml"))
If you get a hash back then your file is fine, otherwise fix the problem that was returned