Railsを使用せずにactiverecordとmysqlを操作しようとしていますが、rubyコードをmysqlに接続できないようです。私の推測では、requireステートメントまたはgemのインストール方法に問題があります。以下であり:
- A.私のスクリプト
- B.宝石がインストールされていることを示す私のローカル宝石リスト
- C.パス変数
- D.私の宝石環境と
- E.私が受け取っているエラーメッセージ
助けてください、これは私の一日を消費しました!(activerecord-mysql-adapterruby-1.9.3-P327@rails3tutorial2ndEd
がオプションの下のジュエリーボックスに表示されていますが、実行時にターミナルに表示されていませんgem list --local
。それと関係がありますか?) (この質問の
解決策も試しました が、機能しませんでした私のため。sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
A.スクリプト:
(シバンの有無にかかわらず試してみました-どちらも機能しませんでした)
1 require 'rubygems'
2 require 'active_record'
3
4 ActiveRecord::Base.establish_connection(
5 :adapter => 'mysql',
6 :host => 'xxxx',
7 :username=> 'xxxx',
8 :password=> 'xxxx',
9 :database=> 'players')
10
11 class Player < ActiveRecord::Base
12 end
13
14 Player.find(:all)
B. gem listを実行したときのターミナルのgems:
$ gem list mysql
*** LOCAL GEMS ***
mysql (2.9.1)
mysql2 (0.3.11)
$ gem list activerecord
*** LOCAL GEMS ***
activerecord (3.2.12, 3.2.8)
C.パス
$ $PATH
-bash: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/bin:/Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327@global/bin:/Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/bin:/Users/emmanueleleyae2/.rvm/bin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin: No such file or directory
$ $GEM_PATH
-bash: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327:/Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327@global: No such file or directory
$ $GEM_HOME
-bash: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327: is a directory
emmanuel-eleyaes-computer:chapter01 emmanueleleyae2$
D.宝石環境
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.25
- RUBY VERSION: 1.9.3 (2012-11-10 patchlevel 327) [x86_64-darwin11.4.0]
- INSTALLATION DIRECTORY: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327
- RUBY EXECUTABLE: /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/bin/ruby
- EXECUTABLE DIRECTORY: /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-11
- GEM PATHS:
- /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327
- /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327@global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
E.エラーメッセージ:
/Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Please install the mysql adapter: `gem install activerecord-mysql-adapter` (Could not find mysql (~> 2.8.1) amongst [actionmailer-3.2.8, actionpack-3.2.8, activemodel-3.2.12, activemodel-3.2.8, activerecord-3.2.12, activerecord-3.2.8, activeresource-3.2.8, activesupport-3.2.12, activesupport-3.2.8, addressable-2.3.2, arel-3.0.2, blankslate-2.1.2.4, bluecloth-2.2.0, builder-3.0.4, bundler-1.2.1, capybara-1.1.2, cgi_multipart_eof_fix-2.5.0, childprocess-0.3.6, coffee-rails-3.2.2, coffee-script-2.2.0, coffee-script-source-1.4.0, daemons-1.1.9, daemons-1.0.10, diff-lcs-1.1.3, erubis-2.7.0, excon-0.16.10, execjs-1.4.0, fastthread-1.0.7, ffi-1.2.0, gem_plugin-0.2.3, gli-2.5.4, heroku-2.33.2, heroku-api-0.3.7, hike-1.2.1, i18n-0.6.1, journey-1.0.4, jquery-rails-2.1.4, jquery-rails-2.1.3, json-1.7.5, launchy-2.1.2, libwebsocket-0.1.7.1, mail-2.4.4, mime-types-1.19, mongrel-1.2.0.pre2, multi_json-1.4.0, multi_json-1.3.7, mysql2-0.3.11, netrc-0.7.7, nokogiri-1.5.5, parslet-1.5.0, pg-0.12.2, polyglot-0.3.3, rack-1.4.1, rack-cache-1.2, rack-protection-1.4.0, rack-ssl-1.3.2, rack-test-0.6.2, rails-3.2.8, railties-3.2.8, rake-10.0.2, rake-0.9.2.2, rdoc-3.12, rest-client-1.6.7, rspec-2.9.0, rspec-core-2.9.0, rspec-expectations-2.9.1, rspec-mocks-2.9.0, rspec-rails-2.9.0, rubygems-bundler-1.1.0, rubyzip-0.9.9, rvm-1.11.3.5, sass-3.2.3, sass-rails-3.2.5, selenium-webdriver-2.27.1, showoff-0.7.0, sinatra-1.3.5, sprockets-2.1.3, sqlite3-1.3.6, sqlite3-1.3.5, thor-0.16.0, tilt-1.3.3, treetop-1.4.12, tzinfo-0.3.35, uglifier-1.3.0, websocket-1.0.4, xpath-0.1.4]) (LoadError)
from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems.rb:1231:in `gem'
from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/mysql_adapter.rb:5:in `<top (required)>'
from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/emmanueleleyae2/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require'
from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:50:in `resolve_hash_connection'
from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:29:in `spec'
from /Users/emmanueleleyae2/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_specification.rb:130:in `establish_connection'
from test.rb:4:in `<main>'