undefined method `namespace' for main:Object (NoMethodError) - active record / rakefile - ruby

I'm attempting to run a basic Sinatra app. When I get to the 'rackup' step I get an error:
/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:3:in `<top (required)>': undefined method `namespace' for main:Object (NoMethodError)
It seems to be a scope issue in the Rake gem. I've had no luck findding an answer and I'm not quite sure what needs to be fixed. I did update all my gems in hopes that would help to no avail. Here is my code that might be contributing....
rakefile.rb
require "./frank"
require "sinatra/activerecord/rake"
config.ru
require_relative 'frank'
map('/welcomes') { run WelcomesController }
frank.rb
require 'sinatra/base'
require 'active_record'
require 'bcrypt'
Dir.glob('./{controllers,models}/*rb').each { |file| require file }
ENV['SINATRA_ENV'] ||= 'development'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => "db/#{ENV['SINATRA_ENV']}.sqlite"
)
spec_helper.rb
ENV['SINATRA_ENV'] = 'test'
require_relative '../frank'
require 'capybara'
require 'database_cleaner'
Capybara.app = Rack::Builder.parse_file(File.expand_path('../../config.ru',__FILE__)).first
RSpec.configure do |config|
config.include Capybara::DSL
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Many thanks :)
This is below the primary error:
from /Users/stephaniedean/.rvm/gems/ruby-2.2.1/gems/sinatra-activerecord-2.0.6/lib/sinatra/activerecord/rake.rb:1:in `load'
from /Users/stephaniedean/.rvm/gems/ruby-2.2.1/gems/sinatra-activerecord-2.0.6/lib/sinatra/activerecord/rake.rb:1:in `<top (required)>'
So, it looks like sinatra-activerecord not just activerecord. I did try activerecord 3.2.17 that didn't work. Thanks for the suggestions.

this took up hours of my time before I found the solution:
http://aaronlerch.github.io/blog/sinatra-bundler-and-the-global-namespace/
https://github.com/sinatra/sinatra-contrib/issues/111
Gemfile
gem "sinatra", require: 'sinatra/base'
gem 'sinatra-activerecord', require: false
gem 'sinatra-contrib', require: false
Environment.rb
require 'bundler/setup'
require 'rake'
require 'sinatra'
require 'sinatra/reloader'
require 'sinatra/activerecord'
require 'sinatra/activerecord/rake'
make sure to
require 'rake'
before
require 'sinatra/activerecord/rake'

Related

sinatra activerecord doesn't find model on heroku

I have a classic style sinatra app (not modular), using sinatra-activerecord.
This is my config.ru
require 'rubygems'
require 'bundler'
Bundler.require
require './config/environment'
require './babycomptes' # main application file
run Sinatra::Application
and config/environment.rb:
APP_ENV = ENV["RACK_ENV"] || "development"
ENV['SINATRA_ENV'] ||= "development"
require 'require_all'
require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
require_all 'app'
The main app file (babycomptes.rb) is sitting at the root of my project. It contains:
require 'sinatra'
require 'sinatra/namespace'
require 'sinatra/activerecord'
require 'bcrypt'
require 'securerandom'
require_relative './app/helpers/helpers.rb'
# Dir["./app/models*.rb"].each {|file| require file }
get '/signup' do
erb :'signup/new'
end
post '/signup' do
#user = User.new(params[:user]) # this is line 51
# cut for brievity
end
The app works fine when I rackup locally and when I run heroku local, but runs into an error on heroku :
2020-12-30T06:20:27.936954+00:00 app[web.1]: 2020-12-30 06:20:27 - NameError - uninitialized constant User:
2020-12-30T06:20:27.936967+00:00 app[web.1]: /app/babycomptes.rb:51:in `block in <top (required)>'
no clue where to start looking....

uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement

Im currently working on a sinatra app, and im having a trouble regarding postgresql connection to sinatra, im try to execute this command:
rake db:create
to create the database but it throws this error.
C:\Users\John\Documents\Registration_Sinatra>rake db:create
rake aborted!
NameError: uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
LoadError: cannot load such file -- sinatra/activerecord
C:/Users/John/Documents/Registration_Sinatra/app/app.rb:2:in `<top (required)>'
C:/Users/John/Documents/Registration_Sinatra/Rakefile:1:in `<top (required)>'
(See full trace by running task with --trace)
this is my app.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'pg'
require './config/environments'
class RegistrationSinatra < ActiveRecord::Base
end
get '/' do
erb :index
end
this is my environments.rb
configure :development do
#DEFAULT_CONN = {database: 'development_registration_sinatra', user: 'postgres', password: 'secret123', host: 'localhost'}
db = URI.parse(ENV['DATABASE_URL'] || "postgres://#{#DEFAULT_CONN[:host]}/#{#DEFAULT_CONN[:database]}?user=#{#DEFAULT_CONN[:user]}")
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => #DEFAULT_CONN[:host],
:username => #DEFAULT_CONN[:user],
:password => #DEFAULT_CONN[:password],
:database => db.path[1..-1],
:encoding => 'utf8')
end
this is my gemfile
source 'https://rubygems.org'
ruby "2.2.2"
gem 'sinatra'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'tux'
gem 'pg'
and my Rakefile
require './app/app'
require 'sinatra/activerecord/rake'
hope you guys can pin point what's wrong with my sample app so i can progress thanks.
Here is the solution: https://github.com/janko-m/sinatra-activerecord/pull/66
In your Gemfile, add:
gem "activerecord", "< 5.0.0"
run bundle update and it will work.

Adapter not set: default. Did you forget to setup?

I'm getting the same error when I execute my test in Rspec. DataMapper::RepositoryNotSetupError: Adapter not set: default. Did you forget to setup? I don't understand this error because I've set up Datamapper as it says in Datamapper webpage. I show you my code
vip_client-spec.rb
require 'spec_helper'
require 'data_mapper'
require 'dm-postgres-adapter'
require File.join(File.dirname(__FILE__), '..', '..', 'models', 'vip_client.rb')
describe VipClient do
before {
DataMapper.finalize.auto_upgrade!
}
describe "#insert_into_database" do
it "inserts clients into database from an array of hashes" do
list_clients = [
{name: "David", birthday: "13-12-1985", email: "daviddsrperiodismo#gmail.com"},
{name: "Javier", birthday: "05-05-1985", email: "javier#gmail.com"}
]
VipClient.insert_into_database(list_clients)
expect(VipClient.all.count).to eq(2)
end
end
end
vip_client.rb
class VipClient
include ::DataMapper::Resource
property :id, Serial
property :name, Text
property :birthday, Date
property :email, Text
def self.insert_into_database(list_clients)
end
end
app.rb
require 'sinatra'
require 'data_mapper'
require 'roo'
require 'pony'
# HELPERS
require './helpers/code'
require './helpers/check_birthday_users'
require './helpers/excel_parser'
# MODELS
require './models/vip_client.rb'
require './models/invitations.rb'
include Code
include CheckUsers
DataMapper.setup(:default, 'postgres://david:123456#localhost/usersmareta')
DataMapper.finalize.auto_upgrade!
And my gemfile is this
source "https://rubygems.org"
gem 'sinatra'
gem 'pg'
gem 'roo'
gem 'data_mapper'
gem 'dm-postgres-adapter'
gem 'pony'
group :development, :test do
gem 'rack-test'
gem 'rspec'
end
As I've said. when I do rspec the console gives me this:
Failure/Error: DataMapper.finalize.auto_upgrade!
DataMapper::RepositoryNotSetupError:
Adapter not set: default. Did you forget to setup?

Bundler.require(:default) fails in Rubinius

I have a very simple app, which i tried to run with Rubinius:
Gemfile:
source 'https://rubygems.org
gem 'rake'
gem 'tiny_tds'
gem 'sequel'
lib/database.rb:
require "rubygems"
require "bundler/setup"
Bundler.require(:default)
DB = Sequel.tinytds(:host => 'SQL2012', :user => "Rails", :password => "xxx", :database => "RailsTest")
test/connection_test.rb:
require_relative "../lib/database"
DB.fetch("SELECT TOP 10 * FROM User") do |row|
puts row.to_s
end
Rakefile:
require 'rubygems'
require 'rake'
require 'rake/clean'
require 'rake/testtask'
Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*.rb']
end
task :default => :test
This runs fine with MRI 1.9.3 and MRI 2.1.0
but fails with rbx 2.2.2 :
klaus#rails-dev:$ rake test
An exception occurred running /home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/rake-10.1.1/lib/rake/rake_test_loader.rb:
no such file to load -- bigdecimal (LoadError)
Backtrace:
Rubinius::CodeLoader#load_error at kernel/common/code_loader.rb:440
Rubinius::CodeLoader#resolve_require_path at kernel/common/code_loader.rb:423
{ } in Rubinius::CodeLoader#require at kernel/common/code_loader.rb:103
Rubinius.synchronize at kernel/bootstrap/rubinius.rb:137
Rubinius::CodeLoader#require at kernel/common/code_loader.rb:102
Rubinius::CodeLoader.require at kernel/common/code_loader.rb:237
Kernel(Object)#require at kernel/common/kernel.rb:705
Object#__script__ at /home2/klaus/.rvm/gems/rbx-2.2.2/gems/tiny_tds-0.6.1/lib/tiny_tds.rb:3
Rubinius::CodeLoader.require at kernel/common/code_loader.rb:243
Kernel.require at kernel/common/kernel.rb:705
{ } in Bundler::Runtime#require at /home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/bundler-1.5.2/lib/bundler/runtime.rb:76
Array#each at kernel/bootstrap/array.rb:66
{ } in Bundler::Runtime#require at /home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/bundler-1.5.2/lib/bundler/runtime.rb:72
Array#each at kernel/bootstrap/array.rb:66
Bundler::Runtime#require at /home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/bundler-1.5.2/lib/bundler/runtime.rb:61
Bundler.require at /home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/bundler-1.5.2/lib/bundler.rb:131
Object#__script__ at lib/database.rb:4
Rubinius::CodeLoader.require at kernel/common/code_loader.rb:243
Rubinius::CodeLoader.require_relative at kernel/common/code_loader.rb:143
Kernel(Object)#require_relative at kernel/common/kernel.rb:711
Object#__script__ at test/connection_test.rb:2
Rubinius::CodeLoader.require at kernel/common/code_loader.rb:243
Kernel(Object)#gem_original_require (require) at kernel/common/kernel.rb:705
Kernel(Object)#require at /home2/klaus/.rvm/rubies/rbx-2.2.2/site/rubygems/core_ext/kernel_require.rb:55
{ } in Object#__script__ at /home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/rake-10.1.1/lib/rake/rake_test_loader.rb:15
{ } in Enumerable(Array)#find_all at kernel/common/enumerable.rb:432
Array#each at kernel/bootstrap/array.rb:66
Enumerable(Array)#select (find_all) at kernel/common/enumerable.rb:430
Object#__script__ at /home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/rake-10.1.1/lib/rake/rake_test_loader.rb:4
Rubinius::CodeLoader#load_script at kernel/delta/code_loader.rb:66
Rubinius::CodeLoader.load_script at kernel/delta/code_loader.rb:200
Rubinius::Loader#script at kernel/loader.rb:649
Rubinius::Loader#main at kernel/loader.rb:831
rake aborted!
Command failed with status (1): [ruby -I"lib" -I"/home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/rake-10.1.1/lib" "/home2/klaus/.rvm/gems/rbx-2.2.2#global/gems/rake-10.1.1/lib/rake/rake_test_loader.rb" "test/connection_test.rb" ]
kernel/bootstrap/proc.rb:20:in `call'
kernel/bootstrap/proc.rb:20:in `call'
kernel/bootstrap/array.rb:66:in `each'
kernel/bootstrap/array.rb:66:in `each'
kernel/common/kernel.rb:447:in `load'
kernel/delta/code_loader.rb:66:in `load_script'
kernel/delta/code_loader.rb:200:in `load_script'
kernel/loader.rb:649:in `script'
kernel/loader.rb:831:in `main'
Tasks: TOP => test
(See full trace by running task with --trace)
You probably need to add bigdecimal or rubysl to your Gemfile. rubinius has gemified their stdlib, causing issues similar to this one.

Unable to make a rake-tasks file and make it work properly

I decided to create a rake tasks for my Sinatra project and not to use the ready ones.
#Rakefile
require 'rake/testtask'
require 'rake/clean'
Dir.glob("tasks/*.rake").each { |r| import r }
#/tasks/seed.rake
require 'rubygems'
require 'bundler'
Bundler.require
require 'mongoid'
require_relative '../models/user'
namespace :db do
task :seed do
puts 'Creating a user....'
user1 = User.new email: "email1#gmail.com", password: "test123"
user1.save!
puts 'User has been created.'
end
end
#user.rb
require 'bcrypt'
require 'digest/md5'
require 'openssl'
class User
include Mongoid::Document
include Mongoid::Timestamps
#.........
#gemfile (partly)
source 'http://rubygems.org'
gem 'bcrypt-ruby', require: 'bcrypt'
And I've got the error of "Creating a user....
rake aborted!
undefined method `create!' for BCrypt::Password:Class
/home/alex/ruby_projects/service/models/user.rb:47:in `password='"
where #47 looks like
def password= pass
self.hashed_password = BCrypt::Password.create! pass, cost: 10
end
Note that in normal development everything works just fine. So I missed to require a file I think.
Your thoughts?
p.s. Even if I put
require 'bcrypt'
require 'digest/md5'
require 'openssl
to /tasks/seed.rake the error remains.
It appears you are using a non-existant method from BCrypt::Password. According to the docs, there is only a .create method and no .create! method. Switch to BCrypt::Password.create and it should work.
def password= pass
self.hashed_password = BCrypt::Password.create pass, cost: 10
end

Resources