【问题标题】:Can I define alias in Rails console我可以在 Rails 控制台中定义别名吗
【发布时间】:2013-07-30 06:26:34
【问题描述】:

我试图编辑我的 ~/.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

【问题讨论】:

    标签: ruby-on-rails-3 rails-console


    【解决方案1】:

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

    module Kernel
      def show_tbls
        ActiveRecord::Base.connection.tables
      end
    end
    

    【讨论】:

    • 请问,我应该修改什么文件?
    • @SaidKaldybaev -- 修改.irbrc 的潜在影响是什么?有吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 2017-04-15
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2011-09-18
    相关资源
    最近更新 更多