Using active record outside rails

Hello People,

I tried this in pry

[2] pry(main)> require ‘mysql2’ => true [3] pry(main)> require ‘active_record’ => true [4] pry(main)> ActiveRecord::Base.establish_connection(:adapter => ‘mysql’, :database => ‘db_name’, :username => ‘root’, :password => ‘root’, :host => ‘localhost’)

And got this error:

LoadError: Could not load ‘active_record/connection_adapters/mysql_adapter’. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than ‘mysql2’, ‘postgresql’ or ‘sqlite3’ add the necessary adapter gem to the Gemfile.

Why do I need a Gemfile for trying active record in irb / pry. Its strange. Is active record so coupled with rails that it cannot be used outside?

ActiveRecord is an actual gem that must be installed ergo why it’s asking for it to be in the Gemfile. Yes it’s tightly coupled with rails but you can absolutely use it in a plain Ruby app. Just install the gems you need and in pry/irb you can call require and it’ll load the library provided that the gem is bundled or installed on your system.

Thanks for the reply, I should have used mysql2 as adapter :wink: Banging my head for not discovering it early :smiley: