undefined method `[]' for nil:NilClass (NoMethodError) - ruby

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??

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.

Related

Minitest: NoMethodError: undefined method `split' for nil:NilClass

This is my test:
test 'accepts nil first_name' do
user = User.new(first_name: nil)
assert_equal(nil, user.first_name)
end
When I run it, I am getting this error from minitest:
NoMethodError: undefined method `split' for nil:NilClass
I can create the user manually in the console and it works, so I'm pretty sure the test should be passing.
Where is this nil.split coming from? My code does not use split anywhere.
Change this:
assert_equal(nil, user.first_name)
To this:
assert_nil(user.first_name)
I didn't dig down the stack deep enough to figure out what was being split where, but this fixed the problem.

NameError: uninitialized constant ActiveModel::Serializers::Xml when declaring a mongomapper document model

This is the first time I use ruby mongodb ORM , and when I follow the tutorial on the website try to make a Document model :
ruby require 'mongo_mapper'
include MongoMapper::Document
key :title, String
key :content, String
key :published_at, Time
timestamps!
end
my command line issue the error
NameError: uninitialized constant ActiveModel::Serializers::Xml
from /Users/RobertRino/.rvm/gems/ruby-2.2.3/gems/mongo_mapper-0.14.0/lib/mongo_mapper/plugins/active_model.rb:9:in'`
and the app crashed.
I searched for solution but seems no one have encountered this problem, could anyone tell me how to solve this error ?
By the way I also try the command above in pry gem.
require 'mongo_mapper'
>>True
MongoMapper.constants
>>[:Error,
:DocumentNotFound,
:InvalidScheme,
:DocumentNotValid,
:AccessibleOrProtected,
:InvalidKey,
:NotSupported,
:Document,
...]
MongoMapper::Document
>> NameError ... (the same error)
Has been moved to external gem, please try to add this to your Gemfile:
gem 'activemodel-serializers-xml'
gem 'active_model_serializers'

`clear_transaction_record_state': undefined method `[]' for nil:NilClass when creating a record Ruby

I'm creating a standalone project in ruby. I wanted to have the same migration as rails so i have installed the gem standalone-migrations. I created my database using this config.yml development:
adapter: postgresql
database: gametour
encoding: utf8
host: localhost
username: tylo
password: ~
then there how i created my first migration :
rake db:new_migration name=flower_type_migration
class FlowerTypeMigration < ActiveRecord::Migration
def change
create_table :flowerTypes do |t|
t.string :type
end
end
end
rake db:migrate
It seems like rake db:migrate works fine since i can see the database and table in psql.
but when i try to create a record in ruby :
FlowerTypes.create(:type => "test"}
i get this error :
/var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:425:in 'clear_transaction_record_state': undefined method '[]' for nil:NilClass (NoMethodError)
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:339:in 'ensure in rollback_active_record_state!'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:339:in 'rollback_active_record_state!'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:318:in 'save'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/suppressor.rb:41:in 'save'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/persistence.rb:34:in 'create'
i have tried to grant privileges to myself (tylo) and to PUBLIC but still get the same error.
I must be missing something, is there any solution to this ?
I have figure out why i'm getting this error. It was because of the "initialize" in my ruby class, i imagine it overwrite the "normal" one.

NoMethodError - undefined method `now' for Watir::Time:Class

I get the following error anytime I try to interact with a Watir element.
/Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/wait/timer.rb:40:in `current_time': undefined method `now' for Watir::Time:Class (NoMethodError)
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/wait/timer.rb:6:in `initialize'
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/elements/element.rb:656:in `new'
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/elements/element.rb:656:in `element_call'
from /Library/Ruby/Gems/2.0.0/gems/watir-6.0.1/lib/watir/elements/element.rb:114:in `click'
from fund_cc.rb:8:in `<main>'
Here is my code:
require 'watir'
# require 'time'
b = Watir::Browser.new(:chrome)#, :url => "http://localhost:9515")
b.goto "https://www.bankofamerica.com/"
contact_us= b.link(:text, "Contact Us")
contact_us.click
Does anyone know how to resolve this?
This should be fixed in version 6.0.2.
From Titus Fortner on the Watir-General mailing list:
The latest version of Watir attempts to use monotomic time where
supported and it looks like we grabbed the wrong Time class for where
it is not supported.
I just updated and pushed the fix to 6.0.2. You should be able to just
bundle update now.
I was able to reproduce this behavior. You can monkey patch your gem locally by tweaking the current_time method in /lib/watir/wait/timer.rb:
def current_time
::Time.now.to_f # was Time.now.to_f
end
And I'd suggest logging an issue on https://github.com/watir/watir/issues.

undefined method `create_db' for #<Mysql:0x000000018f2590> (NoMethodError)

#!/usr/local/bin/ruby
require 'mysql'
db=Mysql.new '127.0.0.1','root','123456'
db.create_db 'testdb'
error message:
test.rb:5:in `<main>': undefined method `create_db' for #<Mysql:0x000000015d0ab0> (NoMethodError)
What's wrong?
Though it is still in the API, create_db cannot be used since mysql4.1.
This function is deprecated. It is preferable to use mysql_query() to
issue an SQL CREATE DATABASE statement instead.
Therefore you need to use query as
require 'mysql'
db = Mysql::new('127.0.0.1','root','123456')
db.query("create database testdb;");
db.close

Resources