Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivierlacan/9548055 to your computer and use it in GitHub Desktop.
Save olivierlacan/9548055 to your computer and use it in GitHub Desktop.
Trying to figure out why I'm having to manually require a class that is within the same namespace inside of Rails.
# located in lib/pumper/nickel.rb
# the only way Back.setup works inside of #add_crappy_music
# is if I add the following:
# require "pumper/nickel/back"
#
# Why?
module Pumper
class Nickel < ActiveRecord::Base
def add_crappy_music
# called within a different class in a different namespace
# this call causes undefined method `setup' for Back:Class
Back.setup
# does not work when calling Pumper::Nickel::Back.setup either
end
end
end
module Pumper
class Nickel < ActiveRecord::Base
class Back < ActiveRecord::Base
def setup
puts "whining"
end
end
end
end
# located in app/services/thing/back_creator.rb
module Thing
class BackCreator
def run
nickel = Pumper::Nickel.new
nickel.add_crappy_music # the failing call originates here
end
end
end
# located in app/models/iron.rb
class Iron < ActiveRecord::Base
# irrelevant
end
@olivierlacan
Copy link
Author

Before anybody asks, /lib is in my config.autoload_paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment