3

The following ought to work as a script, with ActiveRecord 3.1 (note that this is without Rails, not the other way around):

#!/usr/bin/env ruby

require "rubygems"
require "active_record"
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)

irb

Unfortunately, it gives the error:

... connection_specification.rb:71:in `rescue in establish_connection': ...
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` ...

Adding the line gem 'mysql2', '<0.3' before require "active_record" as suggested by some previous posts (which reference 0.2.7, the 0.2 gem at the time; presently it's 0.2.18) doesn't change it.

How can I get it to work? I want ActiveRecord but not the whole of Rails.

3

2 Answers 2

2

Run in terminal

gem install mysql2

and add row to you code require 'mysql2'

#!/usr/bin/env ruby

require "rubygems"
require 'mysql2'
require "active_record"

dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
Sign up to request clarification or add additional context in comments.

1 Comment

Here's how you do it without needing a database.yml file - stackoverflow.com/a/3661927/2229277
0

This has fixed my issue, at least temporarily. I haven't yet restarted, so I don't know if it'll survive that. And it seems like a dirty hack; there's got to be a better way.

I haven't yet tested Aleksei's answer above, since I'm not having the same issue after having run this command. Will update ifwhen I do.

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib ~/.rvm/gems/`rvm current`/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.