【问题标题】:undefined method `[]' for nil:NilClass (NoMethodError)nil:NilClass (NoMethodError) 的未定义方法“[]”
【发布时间】:2013-01-14 13:40:28
【问题描述】:

我正在使用 sequel gem 和 postgresql 数据库

我有一个带有代码的文件 hello.rb

require "rubygems"
require './post'
require "sequel"

# connect to an in-memory database
DB = Sequel.connect('postgres://ritesh:newpassword@localhost')


post = Post['ruby1', 'hello world1']
post[1]

我创建了一个表格帖子和 带代码的模型

require 'sequel'

DB = Sequel.connect('postgres://ritesh:newpassword@localhost')

class Post < Sequel::Model
set_primary_key [:category, :title]

end

当我运行命令 ruby​​ hello.rb 时,我收到以下错误 hello.rb:10: nil:NilClass (NoMethodError) 的未定义方法“[]”

我想知道创建的第一件事是我们应该创建一个具有该名称的表,然后创建一个模型? 第二件事,如果第一个假设是正确的,那么为什么我会收到这个错误??

【问题讨论】:

    标签: ruby postgresql rake sequel


    【解决方案1】:

    要在 ruby​​ 中构造一个新对象,你应该调用 new。我不确定 post 是什么,但似乎正确的语法应该是:

    post = Post.new('ruby1', 'hello world1')
    

    否则帖子将在此行之后保持为空。

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多