4

我试图编辑我的 ~/.irbrc 文件并定义 show_tbls 方法来获取我拥有的表,因为命令太长了,所以我尝试制作一个更容易使用的函数。

require 'hirb' ; Hirb.enable
require 'irb/completion'

def show_tbls
    ActiveRecord::Base.connection.tables
end

当我运行 show_tbls 时,它显示错误如下

1.9.3-p448 :001 > show_tbls()
NoMethodError: undefined method `show_tbls' for main:Object
        from (irb):1
        from /home/poc/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from /home/poc/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from /home/poc/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
1.9.3-p448 :002 > show_tbls
NameError: undefined local variable or method `show_tbls' for main:Object
4

1 回答 1

5

在内核模块中定义你的方法

module Kernel
  def show_tbls
    ActiveRecord::Base.connection.tables
  end
end
于 2013-07-30T09:01:24.587 回答