-1

i am using sequel gem and postgresql database

i have a file hello.rb with code

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]

and i created a table post and a model with code

require 'sequel'

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

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

end

when i run command ruby hello.rb .i am getting the following error hello.rb:10: undefined method `[]' for nil:NilClass (NoMethodError)

first thing i want to know for creating we should create a table with that name and then create a model?? second thing if first assumption is true then why i am getting this error??

Ritesh Mehandiratta
  • 6,595
  • 28
  • 114
  • 186

1 Answers1

0

To construct a new object in ruby you should call new. I am not sure what is post but it seems the correct syntax should be:

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

Otherwise post will remain null after this line.

Ivaylo Strandjev
  • 64,309
  • 15
  • 111
  • 164