I am working on an unpublished gem, which is working correctly in Rails. It has a module that needs to be included inside of ActiveRecord::Base classes.
My Sinatra app looks like this:
# app.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/activerecord'
set :database, "sqlite3:project-name.sqlite3"
get '/' do
#users = User.all
erb :index
end
class User < ActiveRecord::Base
end
My gem file looks like this:
source 'https://rubygems.org'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'sqlite3'
gem 'rake'
gem "my_module", path: "../my_module"
My Gemfile.lock looks like this:
PATH
remote: ../my_module
specs:
my_module (1.49.0)
activerecord (>= 4.2)
GEM
remote: https://rubygems.org/
specs:
activemodel (6.0.2.1)
activesupport (= 6.0.2.1)
activerecord (6.0.2.1)
activemodel (= 6.0.2.1)
activesupport (= 6.0.2.1)
activesupport (6.0.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
concurrent-ruby (1.1.5)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
minitest (5.13.0)
mustermann (1.0.3)
rack (2.0.8)
rack-protection (2.0.7)
rack
rake (13.0.1)
sinatra (2.0.7)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.7)
tilt (~> 2.0)
sinatra-activerecord (2.0.14)
activerecord (>= 3.2)
sinatra (>= 1.0)
sqlite3 (1.4.2)
thread_safe (0.3.6)
tilt (2.0.10)
tzinfo (1.2.5)
thread_safe (~> 0.1)
zeitwerk (2.2.2)
PLATFORMS
ruby
DEPENDENCIES
activerecord
rake
my_module!
sinatra-activerecord
sqlite3
BUNDLED WITH
2.0.2
And if I add the module manually it works fine:
# app.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/activerecord'
set :database, "sqlite3:project-name.sqlite3"
get '/' do
#users = User.all
erb :index
end
module MyModule
extend ActiveSupport::Concern
class_methods do
#something here
end
end
class User < ActiveRecord::Base
include MyModule
end
I expect to be able to do:
# app.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/activerecord'
require 'my_module'
set :database, "sqlite3:project-name.sqlite3"
get '/' do
#users = User.all
erb :index
end
class User < ActiveRecord::Base
include MyModule
end
I get the error
cannot load such file -- my_module (LoadError)
when I use the require statement or
uninitialized constant User::MyModule (NameError)
if I remove require.
It works if I use
bundle exec ruby app.rb
instead of
ruby app.rb
Related
After run rake db:migration I do rake db:migrate, but I don't see in console 'create table...' and when I run sqlite3 .tables , I don't see table
I don't know how to solve this problem. I tried various variants, but not results...
app.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require 'sinatra/activerecord'
set :database, 'sqlite3:barbershop.db'
class Client < ActiveRecord::Base
end
Rakefile:--------------------------
require "./app"
require "sinatra/activerecord/rake"
migration:
class CreateClients < ActiveRecord::Migration[7.0]
def change
create_table :client do |t|
t.text :name
t.text :phone
t.text :datestamp
t.text :barber
t.text :color
end
end
end
Gemfile:-----------------------
source "https://rubygems.org"
gem "sinatra"
gem "sqlite3"
gem "activerecord"
gem "sinatra-activerecord"
gem "sinatra-contrib"
gem "rake"
gem "puma"
group :development do
gem "tux"
end
Gemfile.lock:------------------
GEM
remote: https://rubygems.org/
specs:
activemodel (7.0.4.2)
activesupport (= 7.0.4.2)
activerecord (7.0.4.2)
activemodel (= 7.0.4.2)
activesupport (= 7.0.4.2)
activesupport (7.0.4.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
backports (3.23.0)
bond (0.5.1)
concurrent-ruby (1.2.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
mini_portile2 (2.8.1)
minitest (5.17.0)
multi_json (1.15.0)
nio4r (2.5.8)
puma (6.1.0)
nio4r (~> 2.0)
rack (1.5.2)
rack-protection (1.5.1)
rack
rack-test (0.6.3)
rack (>= 1.0)
rake (13.0.6)
ripl (0.7.1)
bond (~> 0.5.1)
ripl-multi_line (0.3.1)
ripl (>= 0.3.6)
ripl-rack (0.2.1)
rack (>= 1.0)
rack-test (~> 0.6.2)
ripl (>= 0.7.0)
sinatra (1.4.4)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
sinatra-activerecord (2.0.26)
activerecord (>= 4.1)
sinatra (>= 1.0)
sinatra-contrib (1.4.7)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (>= 1.3, < 3)
sqlite3 (1.6.0)
mini_portile2 (~> 2.8.0)
sqlite3 (1.6.0-x64-mingw32)
tilt (1.4.1)
tux (0.3.0)
ripl (>= 0.3.5)
ripl-multi_line (>= 0.2.4)
ripl-rack (>= 0.2.0)
sinatra (>= 1.2.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
PLATFORMS
ruby
x64-mingw32
DEPENDENCIES
activerecord
puma
rake
sinatra
sinatra-activerecord
sinatra-contrib
sqlite3
tux
BUNDLED WITH
2.4.3
I'm currently trying to write a very basic portion of code that reads a ruby file and instantiate some documents into my local MongoDB.
The class modeling my Mongo document is in a separate file called 'search_term.rb', as follows:
class SearchTerm
include Mongoid::Document
field :search_term, type: String
end
In a separate file called 'populate_database.rb' that resides in the same folder as 'search_term.rb', I'm trying to read from a list and create new documents and look into my MongoDB Compass if they are actually being created:
require_relative 'search_term'
business_list = [
'business name 1',
'business name 2',
'business name 3',
'business name 4',
]
for business in business_list
s_term = SearchTerm.new()
s_term.search_term = business
s_term.post
end
The problem is: when I run 'ruby populate_database.rb' I'm getting the error:
<class:SearchTerm>': uninitialized constant SearchTerm::Mongoid (NameError)
I have already tried creating an empty Gemfile and writing:
gem 'mongo'
gem 'mongoid'
into them and running 'bundle install'. After that the following Gemfile.lock file is created:
GEM
specs:
activemodel (6.1.4.1)
activesupport (= 6.1.4.1)
activesupport (6.1.4.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
bson (4.12.1)
concurrent-ruby (1.1.9)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
minitest (5.14.4)
mongo (2.13.0)
bson (>= 4.8.2, < 5.0.0)
mongoid (7.3.2)
activemodel (>= 5.1, < 6.2)
mongo (>= 2.10.5, < 3.0.0)
ruby2_keywords (~> 0.0.5)
ruby2_keywords (0.0.5)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
zeitwerk (2.4.2)
PLATFORMS
x64-mingw32
DEPENDENCIES
mongo
mongoid
BUNDLED WITH
2.2.27
But the problem still persists. Can you guys give me some advice on how to solve this one?
Thanks in advance!!
If you are not using Rails, you need to require "mongoid" yourself somewhere in your application and then load Mongoid configuration. See here for an example of how to do that.
This is my first attempt with a ruby stack. I'm stuck with the following error:
Could not find rack-1.6.4 in any of the sources (Bundler::GemNotFound)
I've successfully installed the following components:
ubuntu 14.04.3 LTS
rvm 1.26.11
ruby 2.1.6p336
nginx 1.8
* LOCAL GEMS *
bigdecimal (1.2.4)
bundler (1.10.6)
bundler-unload (1.0.2)
executable-hooks (1.3.2)
gem-wrappers (1.2.7)
io-console (0.4.3)
json (1.8.1)
minitest (4.7.5)
psych (2.0.5)
rack (1.6.4)
rack-protection (1.5.3)
rake (10.1.0)
rdoc (4.1.0)
rubygems-bundler (1.4.4)
rvm (1.11.3.9)
sinatra (1.4.6)
test-unit (2.1.6.0)
tilt (2.0.1)
* Gemfile *
gem 'sinatra', '1.4.6'
* Gemfile.lock *
GEM
remote: https://rubygems.org/
specs:
rack (1.6.4)
rack-protection (1.5.3)
rack
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.1)
PLATFORMS
ruby
DEPENDENCIES
sinatra (= 1.4.6)
BUNDLED WITH
1.10.6
* config.ru *
require './app.rb'
run Sinatra::Application
* app.rb *
require 'bundler/setup'
require 'sinatra'
get '/' do
'hello world'
end
I'm using the default Nginx folder for the app. I was able to execute the following test in config.ru:
app = proc do |env|
[200, { "Content-Type" => "text/html" }, ["hello world"]]
end
But as soon I try to switch to Sinatra I get the error above.
Thanks in advance!
Ok I found the problem.
I followed the installation instruction here: https://www.phusionpassenger.com/library/install/nginx/install/oss/trusty/
But since I'm using rvm I had to change the passenger_ruby directive to point to the rvm wrapper: /usr/local/rvm/wrappers/ruby-2.1.6/ruby
Problem is here:
rack (1.6.4)
...
sinatra (1.4.6)
rack (~> 1.4)
You have a conflict in the rack version. you want both 1.6.4 and 1.4.x
How did rack 1.6.4 end up in the gemfile.lock?
I'm trying to format my output as json. Here is a test code from http://www.sinatrarb.com/contrib/json.html :
require "sinatra"
require "sinatra/json"
# define a route that uses the helper
get '/' do
json :foo => 'bar'
end
# The rest of your classic application code goes here...
It shows me an "Application Error". Maybe it comes from the other files to launch the app. I'm using heroku(cloud). So I have a Gemfile :
source 'https://rubygems.org'
gem 'sinatra', '1.1.0'
gem 'thin'
Gemfile.lock :
GEM
remote: https://rubygems.org/
specs:
daemons (1.1.9)
eventmachine (1.0.3)
rack (1.5.2)
sinatra (1.1.0)
rack (~> 1.1)
tilt (~> 1.1)
thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.4.1)
PLATFORMS
ruby
DEPENDENCIES
sinatra (= 1.1.0)
thin
and Procfile :
web: bundle exec ruby web.rb -p $PORT
Did I miss something ?
You need to reference in your Gemfile json gem, e.g:
gem "json", "1.5.5"
And then you call .to_json on the object you want to output as JSON.
I'm starting my first Sinatra App and I'm trying to use DataMapper. Everything is in the very early stages, as I can't get it to actually create the DB. I get "LoadError: no such file to load -- dm-sqlite-adapter" when I try to visit my page.
Here's the code from my Sinatra App so far:
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-timestamps'
# Also tried require 'datamapper' , but the same issue shows up
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/raffle.db")
class Raffle
include DataMapper::Resource
property :id, Serial
property :firstName, String
property :lastName, String
property :email, String
property :created_at, DateTime
end
# Create, upgrade, or migrate DB Tables
DataMapper.auto_upgrade!
I have the gems installed, as gem list outputs:
*** LOCAL GEMS ***
activemodel (3.0.9, 3.0.3)
activerecord (3.0.9, 3.0.3)
activesupport (3.0.9, 3.0.3)
addressable (2.2.6)
arel (2.0.10, 2.0.4)
bcrypt-ruby (2.1.4)
builder (2.1.2)
bundler (1.0.15)
data_objects (0.10.6)
datamapper (1.1.0)
diff-lcs (1.1.2)
dm-aggregates (1.1.0)
dm-constraints (1.1.0)
dm-core (1.1.0)
dm-migrations (1.1.0)
dm-serializer (1.1.0)
dm-timestamps (1.1.0)
dm-transactions (1.1.0)
dm-types (1.1.0)
dm-validations (1.1.0)
do_sqlite3 (0.10.6)
fastercsv (1.5.4)
ffi (0.6.3)
i18n (0.5.0, 0.4.2)
json (1.5.3, 1.4.6)
mime-types (1.16)
rack (1.3.0, 1.2.1)
rack-test (0.5.6)
rake (0.8.7)
require_all (1.2.0)
rspec (2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
rspec-mocks (2.6.0)
shotgun (0.9)
sinatra (1.2.6, 1.1.0)
sqlite3 (0.1.1)
stringex (1.2.1)
tilt (1.3.2, 1.1)
typhoeus (0.2.4, 0.2.0)
tzinfo (0.3.29, 0.3.23)
uuidtools (2.1.2)
Any advice/insight is always appreciated.
I don't see the dm-sqlite-adapter gem in that list. Try installing it.
It may be a bit late for the original question, but in case anyone has a similar issue, putting an underscore "data_mapper" in require 'data_mapper' worked for me.
You always need to explicitly add an adapter gem to your gemfile. Even data_mapper meta gem doesn't require any of the adapters.