macsig macsig - 2 months ago 16
Ruby Question

Requiring ActiveRecord on IRB - Ruby (NO Rails)

How can I load

ActiveRecord
on an IRB session?

I have the following

# config/app.rb
require 'active_record'

ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/mydb.sqlite3'
)


But when I start IRB and try to load it

irb#1(main):001:0> require config/application.rb


I get

NameError: undefined local variable or method `config' for main:Object
Did you mean? conf


I'd like to be able to interact with my ActiveRecord objects from IRB.
I'm NOT using Rails but only ActiveRecord.

Thanks

Answer

Two things to change here:

  1. Always put quotes around the path you're requiring. The reason Ruby is saying "undefined local variable or method" is that it's trying to interpret config as a variable name. Put the name in quotes and it won't.
  2. Use require_relative when loading files that are part of your application. require only looks in the default

    require_relative 'config/application.rb'
    
Comments