uninitialized constant ActiveRecord::ConnectionAdapters::ConnectionManagement - ruby

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.

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

Using gems in Ruby *basic*

I'm a complete newbie.
I have a gem I want to install and use, let's say it's this one:
https://rubygems.org/gems/linkedindata/versions/0.0.22
I'm using cmd:
gem install linkedindata
#1 gem installed
After that I add it to my gemfile:
gemrat 'linkedindata'
#gem 'linkedindata', '0.0.22' added to your Gemfile.
Now to use linkedindata I have to create a new object and specify my search. So I do:
**test.rb**
l = LinkedinData.new(1, "c:/users/proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
Now I run the test.rb from command prompt:
test.rb:1:in `<main>': undefined local variable or meth
od `linkedindata' for main:Object (NameError)
So, I obviously need to require the 'linkedindata' gem here. I added:
**test.rb**
require 'LinkedinData'
l = LinkedinData.new(1, "c:/users/proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
I get the following error:
C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `re
quire': cannot load such file -- linkedin-scraper (LoadError)
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:54:in `require'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin
data.rb:1:in `<top (required)>'
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:128:in `require'
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:128:in `rescue in require'
from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require
.rb:39:in `require'
from C:/Users/test.rb:1:in `<main>'
Am I doing something fundamentally wrong here? Or is there a problem with this gem?
UPDATE
The problem was within the gem, see Doon's answer below.
require 'rubygems'
require 'bundler/setup'
require 'linkedindata'
l = LinkedinData.new(1, "proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
++++ correct how the gem requires "linkedin_scraper" in linkedindata.rb
Next problem occurs
C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin.rb:6:in `<cl
ass:Profile>': uninitialized constant Linkedin::Profile::ProxyManager (NameError
)
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin
.rb:5:in `<module:Linkedin>'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/linkedindata-0.0.22/lib/linkedin
.rb:4:in `<top (required)>'
from linkedinscrape.rb:4:in `require'
from linkedinscrape.rb:4:in `<main>'
Which obviously has to do with the proxy settings. The list:
**proxylist.txt**
220.248.224.242:8089
165.139.179.225:8080
54.207.114.172:3333
190.63.174.246:8081
The way I load it:
l = LinkedinData.new(1, "c:/users/proxylist.txt")
As you are using a Gemfile, you are using bundler. In your script you will need to include rubygems and bundler to make it work. Try this
require 'rubygems'
require 'bundler/setup'
require 'linkedindata'
l = LinkedinData.new(1, "c:/users/proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
You have run bundle install, sorry not familiar with gemrat, so not sure how much it does for you.
if you are not actually using bundler, just remove require 'bundler/setup' from the code above, you will still need to require rubygems though in your script.
in looks like the gem itself is kind of broken in the way it declare dependencies, and the other gems that it relies on. I appear to be able to make it work as follows:
Gemfile:
source 'https://rubygems.org'
gem 'linkedin-scraper'
gem 'linkedindata'
gem 'generalscraper'
gem 'uploadconvert'
gem 'docsplit'
gem 'crack'
gem 'pry'
gem 'activesupport'
gem 'selenium-webdriver'
It also appears that it the linkedin-scraper needs to required as linkedin_scraper but not sure why that is. So editing.
{GEMPATH}/linkedindata-0.0.22/lib/linkedindata.rb to require 'linkedin_scraper' as opposed to require 'linkedin-scraper' seems to make it work.
So with the above changes and using bundler
require 'rubygems'
require 'bundler/setup'
require 'linkedindata'
l = LinkedinData.new(1, "proxylist.txt", true, true)
searchTerms = ['First', 'Second', 'Third']
l.getByKeywords(searchTerms)
now runs (I don't have a proxylist.txt so it blows up looking for it, but it doesn't get anymore library errors).

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

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'

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.

Issues with mysql2 with and ruby

Trying to get the gem mysql2 installed on ubuntu and I have tried all of the suggestions but I cannot get it to run. Here is the error in my application.
./bla.rb:65:in `post_init': undefined method `query' for nil:NilClass (NoMethodError)
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/em/timers.rb:51:in `call'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/em/timers.rb:51:in `fire'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from ./bla.rb:234:in `start_server'
from ./bin/minibardaemon:15
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:254:in `call'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:254:in `start_proc'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:263:in `call'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:263:in `start_proc'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:295:in `start'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/controller.rb:73:in `run'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:197:in `run_proc'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `call'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `catch_exceptions'
from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:196:in `run_proc'
I have installed all of the packages that are recommended and installed mysql2 via gem but still no luck.
libmysqlclient-dev
Is installed.
Im on Ubuntu.
# gem -v
1.3.7
# ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
Installing the gems
# gem install mysql
Building native extensions. This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...
Installing RDoc documentation for mysql-2.8.1...
# gem install mysql2
Building native extensions. This could take a while...
Successfully installed mysql2-0.3.11
1 gem installed
Installing ri documentation for mysql2-0.3.11...
Installing RDoc documentation for mysql2-0.3.11...
def create_mysql2
begin
mysql2 = Mysql2::EM::Client.new(
:username => DBUSER,
:password => DBPASS,
:host => DBHOST,
:port => DBPORT,
:socket => DBSOCKET,
:database => DBNAME
)
return mysql2
rescue Mysql2::Error => exception
$stderr.puts "Mysql Error: #{ exception.message }"
EventMachine::stop_event_loop
end
end
...
begin
mysql2 = create_mysql2
rescue Exception => ex
p ex
end
# query the db every x seconds.
EventMachine::add_periodic_timer(QUERY_INTERVAL) do
defer1 = mysql2.query "SELECT * FROM table LIMIT #{QUERY_LIMIT}"
doesn't realy look like mysql2 gem issue, could you please show 65'th line of you bla.rb file? And probably some number of lines that surround it.
it actually looks like your mysql2 client gets uninitialized for some reason.
I would advice to check if something like this works:
require 'mysql2'
mysql_client = Mysql2::Client.new(:host => "localhost", :username => "root")
mysql_client.query('sql .. ')
if so, there's clearly an issue in your script

Resources