her icon indicating copy to clipboard operation
her copied to clipboard

undefined method `request' for nil:NilClass

Open mbajur opened this issue 11 years ago • 5 comments
trafficstars

Hello there! I'm trying to use her gem in my application (rails4 + ruby2) and everything seems to work fine. Until i'll try to use it in module namespace. For example:

class Video
  include Her::Model
end

Video.find(4)

Works just fine but:

module Depot
  class Video
    include Her::Model
  end
end

Depot::Video.find(4)

This throws me an error:

undefined method `request' for nil:NilClass

# Stacktrace
her (0.6.8) lib/her/model/http.rb:52:in `request'
her (0.6.8) lib/her/model/relation.rb:95:in `block in find'
her (0.6.8) lib/her/model/relation.rb:88:in `map'
her (0.6.8) lib/her/model/relation.rb:88:in `find'
her (0.6.8) lib/her/model/orm.rb:127:in `find'
app/controllers/application_controller.rb:45:in `some_method'

some_method() is a method which fires Depot::Video.find(4) line and there is no other code in it.

Any ideas?

mbajur avatar Dec 06 '13 18:12 mbajur

I'm experiencing this error too. Any input would be appreciated.

irb(main):011:0> Transistor::Channel.all.to_a
NoMethodError: undefined method `request' for nil:NilClass
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/her-0.6.8/lib/her/model/http.rb:52:in `request'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/her-0.6.8/lib/her/model/relation.rb:70:in `fetch'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/her-0.6.8/lib/her/model/relation.rb:45:in `method_missing'
    from (irb):11
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/cli.rb:619:in `console'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/vendor/thor/task.rb:27:in `run'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/vendor/thor/invocation.rb:120:in `invoke_task'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/vendor/thor.rb:344:in `dispatch'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/vendor/thor/base.rb:434:in `start'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/bin/bundle:20:in `block in <top (required)>'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/friendly_errors.rb:3:in `with_friendly_errors'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/bin/bundle:20:in `<top (required)>'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/bin/bundle:23:in `load'
    from /Users/nicholaswyoung/.rbenv/versions/2.0.0-p353/bin/bundle:23:in `<main>'

secretfader avatar Jan 08 '14 16:01 secretfader

Looks like the order in which the files get loaded get's screwed up and Her::API.setup doesn't get run first.

I was able to fix it in this really hacky way:

module Client
  class Base
    Her::API.setup
    include Her::Model
  end
end

aalvarado avatar Feb 15 '14 06:02 aalvarado

Hi @mbajur,

I know it has been a while but are you still experiencing this issue? This code works just fine for me:

module Test
  class User
    include Her::Model
  end
end

u = Test::User.all

u.each do |user|
  puts user.message
end

Chris911 avatar May 01 '14 03:05 Chris911

I was able to solve it by doing require false in the Gemfile, and load the api and the Base on initializer.

aalvarado avatar May 01 '14 13:05 aalvarado

I was having the same problem. Since I'm using Her in a rubygem, "require: false" isn't an option AFAIK.

Still, I was able to fix the problem by ensuring that Her initialization happened before my model was loaded. Unfortunate, but at least it works.

mkb avatar Sep 15 '14 22:09 mkb