macsig macsig - 1 year ago 68
Ruby Question

Requiring ActiveRecord on IRB - Ruby (NO Rails)

How can I load

on an IRB session?

I have the following

# config/app.rb
require 'active_record'

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.


Answer Source

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'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download