Hello every body, i have met a strange issue about activerecord gem.
This is my simple code:
in the "interesting" file name ~/ruby/mysql.rb
===========================
#!/usr/bin/ruby
require "rubygems"
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "database",
)
class Text < ActiveRecord::Base
end
Text.create
title => 'hello')
===========================
When i execute it in home dir, it works well.
======
abc@abc-desktop:~$ pwd
/home/abc
abc@abc-desktop:~$ ruby/mysql.rb
/var/lib/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/connection_adapters/mysql_adapter.rb:619:in
`real_connect': Access denied for user 'root'@'localhost' (using
password: NO) (Mysql::Error)
======
And then i change the work dir, it returns "Load Error".
======
abc@abc-desktop:~/ruby$ pwd
/home/abc/ruby
abc@abc-desktop:~/ruby$ ./mysql.rb
/var/lib/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/connection_adapters/mysql_adapter.rb:7:in
`define_all_hashes_method!': Mysql not loaded (RuntimeError)
======
Who can tell me how this happend?
It took me some time to figure out, but actually this is somewhat funny ...
At some point, the ActiveRecord code calls "require 'mysql'"
(/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb
line 20).
In the case where you run from the home dir, this works our correctly
(and loads the correct code), but when you run it from the ~/ruby
directory, this actually reloads your own ~/ruby/mysql.rb program again ...
(and this version of mysql.rb does not have the required methods).
A strong hint was in the remainder of the exception log below (it is always
better to post the full error log to the list and not just the top line).
The simple solution is to rename you program, e.g. to test_mysql.rb
peterv@ASUS:~/ruby$ ./test_mysql.rb
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:600:in
`real_connect': Access denied for user 'root'@'localhost' (using
password: NO) (Mysql::Error) ## this is the expected behavior
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:600:in
`connect'
...
Original trace below.
HTH,
Peter
peterv@ASUS:~/ruby$ ./mysql.rb
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:26:in
`mysql_connection': !!! Outdated mysql gem. Upgrade to 2.8.1 or later.
In your Gemfile: gem 'mysql', '2.8.1'. Or use gem 'mysql2'
(RuntimeError)
## (BTW, I do have the 2.8.1 and the mysql2 gem, just a side effect of
the root cause problem)
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in
`send'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in
`new_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in
`checkout_new_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in
`checkout'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in
`loop'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in
`checkout'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in
`checkout'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:93:in
`connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in
`retrieve_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in
`retrieve_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in
`connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:679:in
`columns'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/persistence.rb:284:in
`attributes_from_column_definition'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/locking/optimistic.rb:62:in
`attributes_from_column_definition'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1394:in
`initialize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:496:in
`new'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:496:in
`create'
from ./mysql.rb:14
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in
`require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in
`load_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in
`new_constants_in'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in
`load_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in
`require'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/mysql_adapter.rb:20:in
`mysql_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in
`send'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:228:in
`new_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in
`checkout_new_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in
`checkout'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in
`loop'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:186:in
`checkout'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in
`checkout'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:93:in
`connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in
`retrieve_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:97:in
`retrieve_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in
`connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:679:in
`columns'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/persistence.rb:284:in
`attributes_from_column_definition'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/locking/optimistic.rb:62:in
`attributes_from_column_definition'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1394:in
`initialize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:496:in
`new'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:496:in
`create'
from ./mysql.rb:14