With the buzz around Ruby on Rail, I have decided to try and start learning. I hit quite a few road blocks just on my way setting it up. Here are some lesson learnt:
Installing RoR isn’t hard at all. I’m on Mac OSX.
1) Install RoR using the command “gem update rails” in the terminal.
2) Install RubyGem from http://docs.rubygems.org/ and run “ruby setup.rb”
3) Simply run “gem install rails” and basically you’re done.
4) Create a new project by the command “rails new path/to/your/new/application”
5) Go to the new project with command “cd path/to/your/new/application”
6) Start the server with “rails server”.
7) Go to localhost:3000 and it works like a charm with the default page.
http://rubyonrails.org/download is where you can find the full instruction on how to start.
The default setting uses sqlite3 as database. I want to set it up with mysql. Since I already have MAMP installed, I tried connecting it to the mysql database there.
I changed config/database.yml replacing the development section with the following:
development:
adapter: mysql2
database: ToDo
host: localhost
username: ruby
password: ruby
After changing database.yml, I tried reloading localhost:3000 and got the error “ActiveRecord::ConnectionNotEstablished”
From all the Google research, it seems to be a connection to database issue. Here are the steps I have to resolve the issue:
Made sure I have mysql installed and made sure I install mysql gem correctly.
1) I did the command ‘sudo gem install mysql’
but I had the following errors:
checking for mysql_ssl_set()… no
checking for mysql.h… no
checking for mysql/mysql.h… no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
–with-opt-dir
–without-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
–with-mysql-config
I tried using the command to specify the location of the config but since MAMP’s mysql does not have an include folder, it doesn’t work. Then I found instruction here to recompile MAMP for Ruby.
Here are the steps:
- Assumed you already have MAMP installed.
- Download the MAMP 1.7.2 source code.
- Unzip the source code file.
- Open the terminal and go into the source code directory.
- Untar the mysql file and go into the untarred directory:
$ tar -xzvf mysql-5.0.41.tar.gz
$ cd mysql-5.0.41
- This is the Hootbah magic…we’re basically compiling libraries here for the Gem to link against.
$ ./configure –with-unix-socket-path=/Applications/MAMP/tmp/mysql/mysql.sock –without-server –prefix=/Applications/MAMP/Library
$ make -j2
- Copy the compiled libraries into MAMP:
$ cp libmysql/.libs/*.dylib /Applications/MAMP/Library/lib/mysql
- Copy the MySQL headers into MAMP…Hootbah didn’t do this but I had been doing it earlier trying to get around some missing mysql.h problems, and figured I’d keep doing it:
$ mkdir /Applications/MAMP/Library/include
$ cp -R include /Applications/MAMP/Library/include/mysql
- Install the Ruby MySQL Gem:
$ sudo env ARCHFLAGS=”-arch i386″ gem install mysql — –with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
After all these are done, seems like it does the magic in fixing the issues I have before but I started having a new error:
dyld: lazy symbol binding failed: Symbol not found: _mysql_get_client_info Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle Expected in: flat namespace
dyld: Symbol not found: _mysql_get_client_info Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle Expected in: flat namespace
Trace/BPT trap
Back to research.
Tried a few workaround. Some posts online suggested to copied or link the .lib and that didn’t work. That error seems to be saying that something’s wrong with the mysql bundle.
More researching suggested that maybe it’s mysql and ruby not matching. They need to be both either 32 bits version or 64 bits version. I double checked the mysql in MAMP and it’s 32 bits version.
So redoing installation again. I downloaded 64 bits version mysql and installed on my computer. Reinstall mysql gem using the following command:
sudo env ARCHFLAGS=”-arch x86_64″ gem install mysql2 –version ‘~> 0.2.7′ — –with-mysql-config=/usr/local/mysql/bin/mysql_config
Same error:
ActiveRecord::ConnectionNotEstablished
Of course, just the beauty of copy and paste and missed the very obvious. Last bit of research, change in Gemfile from mysql to mysql2 (since the gem install command I did was for sql2)
After that,
$rails server
My RoR with mysql is up and running!
Continuing my tutorial, I tried creating the table using the following command after creating the migrate script:
$rake db:migrate
and I got the following errors:
$ sudo rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
rake aborted!
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2. Make sure all dependencies are added to Gemfile.)
/Library/Ruby/Gems/1.8/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in `establish_connection'
/Library/Ruby/Gems/1.8/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:60:in `establish_connection'
/Library/Ruby/Gems/1.8/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
/Library/Ruby/Gems/1.8/gems/activerecord-3.1.3/lib/active_record/railtie.rb:69
/Library/Ruby/Gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
/Library/Ruby/Gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/Library/Ruby/Gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
/Library/Ruby/Gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:42:in `each'
/Library/Ruby/Gems/1.8/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/Library/Ruby/Gems/1.8/gems/activerecord-3.1.3/lib/active_record/base.rb:2190
/Library/Ruby/Gems/1.8/gems/activerecord-3.1.3/lib/active_record/railties/databases.rake:6
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `invoke_prerequisites'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:157:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/francessmli/.gem/ruby/1.8/gems/rake-0.9.2.2/bin/rake:33
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
Tasks: TOP => db:migrate => db:load_config
Just with a bit of research and using the following command to see more details of what’s wrong:
$rake db:migration –trace
Turned out the mysql is not the correct version.
In order resolve this, I did the followings:
1) $ gem uninstall mysql2
2) $ sudo env ARCHFLAGS=”-arch x86_64″ gem install mysql2 –version ‘~> 0.3.6′ — –with-mysql-config=/usr/local/mysql/bin/mysql_config
3) Modified Gemfile from “gem ‘mysql2′,’0.2.7′” to “gem ‘mysql2′,’0.3.6′”
4) $ bundle install
Off I go with my RoR tutorial!