在IRB上需要ActiveRecord-Ruby(无Rails) [英] Requiring ActiveRecord on IRB - Ruby (NO Rails)

查看:75
本文介绍了在IRB上需要ActiveRecord-Ruby(无Rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在IRB会话上加载 ActiveRecord

How can I load ActiveRecord on an IRB session?

我有以下

# config/app.rb
require 'active_record'

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

但是当我启动IRB并尝试加载它时

But when I start IRB and try to load it

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

我得到

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

我希望能够与来自IRB的ActiveRecord对象进行交互。
我不是在使用Rails,而是在使用ActiveRecord。

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

谢谢

推荐答案

这里有两点要更改:


  1. 始终在所需的路径周围加上引号。 Ruby之所以说未定义的局部变量或方法,是因为它试图将 config 解释为变量名。

  2. 在加载属于应用程序的文件时,使用 require_relative 要求仅在默认的Ruby加载路径中查找。

  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 Ruby load paths.

尝试以下操作:

    require_relative 'config/application.rb'

这篇关于在IRB上需要ActiveRecord-Ruby(无Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆