Skip to content

Instantly share code, notes, and snippets.

@tmichel
Created July 6, 2015 14:14
Show Gist options
  • Select an option

  • Save tmichel/fa5548f43cc729cb8f0d to your computer and use it in GitHub Desktop.

Select an option

Save tmichel/fa5548f43cc729cb8f0d to your computer and use it in GitHub Desktop.
Rails constant lookup WTF
# Rails autoloading constant functionality is useful until it's not.
# Every class is placed in the right file on the right path.
# in app/models/users/serializer.rb
module Users
class Serializer
#...
end
end
# in app/models/users/publish.rb
module Users
class Publish
# this works
def self.user(user)
publish(Serializer.new(user))
end
class << self
# this does not work, Rails cannot lookup Serializer constant
def user(user)
publish(Serilaizer.new(user)
end
# this does work because the full name of the class is specified
def user(user)
publish(Users::Serializer.new(user))
end
end
def self.publish(ser)
# ...
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment