-1

私は続編の宝石と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: undefined method `[]' for nil:NilClass (NoMethodError) が表示されます

作成について最初に知りたいことは、その名前でテーブルを作成してからモデルを作成することです?? 2番目に、最初の仮定が正しい場合、なぜこのエラーが発生するのですか??

4

1 に答える 1

0

Ruby で新しいオブジェクトを作成するには、new を呼び出す必要があります。投稿が何であるかはわかりませんが、正しい構文は次のようになります。

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

そうしないと、この行以降の投稿は null のままになります。

于 2013-01-30T16:43:12.790 に答える