加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 我可以在Rails控制台中定义别名

发布时间:2020-12-17 02:39:00 所属栏目:百科 来源:网络整理
导读:我试着编辑我的?/ .irbrc文件 并定义show_tbls方法来获取我拥有的表, 因为命令很长,所以我试着让函数更容易使用. require 'hirb' ; Hirb.enablerequire 'irb/completion'def show_tbls ActiveRecord::Base.connection.tablesend 当我运行show_tbls时 它显示
我试着编辑我的?/ .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

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读