0

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Timmy Von Heiss
  • 2,160
  • 17
  • 39

1 Answers1

0

It works if I use

bundle exec ruby app.rb

instead of

ruby app.rb
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Timmy Von Heiss
  • 2,160
  • 17
  • 39
  • It's important to do more than toss out code in an answer. Pointing to documentation and referencing the most significant information helps educate those looking for a similar solution in the future, which is why SO exists. https://bundler.io/v2.0/man/bundle-exec.1.html – the Tin Man Dec 31 '19 at 22:07