Using active record outside rails

63 views
Skip to first unread message

Karthikeyan A K

unread,
Sep 28, 2016, 7:57:53 AM9/28/16
to rubyonra...@googlegroups.com
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?

James Jelinek

unread,
Sep 28, 2016, 8:10:59 AM9/28/16
to rubyonra...@googlegroups.com
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. 

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAJR%2B9kbwhPgkXQ0RzA2AtmkiWmqnNak8TD%3DBp2E1d-rZC6WOzw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Karthikeyan A K

unread,
Sep 28, 2016, 9:39:10 AM9/28/16
to rubyonra...@googlegroups.com
Thanks for the reply, I should have used mysql2 as adapter ;) Banging my head for not discovering it early :D

On Wed, Sep 28, 2016 at 5:40 PM, James Jelinek <jel...@gmail.com> wrote:
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. 

Sent from my iPhone

On Sep 28, 2016, at 6:53 AM, Karthikeyan A K <77m...@gmail.com> wrote:

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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0FBA9E0A-5775-4CC7-8488-9F0B5BD2DD5F%40gmail.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages