如何在IRB中访问哈希的公共类方法

时间:2014-08-14 11:44:17

标签: ruby-on-rails irb

在这里开始RoR程序员......

在IRB中,我有一个从数据库表中检索的哈希。我想在这个哈希(.select,.keys等)上执行Hash类方法,但是当我尝试这样做时,我得到NoM​​ethodError。我在这里错过了什么基本概念?

编辑:这是控制台输出

2.0.0-p247 :020 > vote_record.keys                                                                                                                                                                                                                                                
NoMethodError: undefined method `keys' for #<Vote:0x00000002736f98>                                                                                                                                                                                                               
    from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-       4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing'                                                                                                                                          
    from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:131:in `method_missing'                                                                                                                                        
    from (irb):20                                                                                                                                                                                                                                                             
    from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'                                                                                                                                                               
    from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'                                                                                                                                                                
    from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'                                                                                                                                                            
    from bin/rails:4:in `require'                                                                                                                                                                                                                                             
    from bin/rails:4:in `<main>

1 个答案:

答案 0 :(得分:0)

您的vote_record不是Hash,而是Vote(继承自ActiveRecord::Base类)实例。