When not to use `self.class.name`
When logging information or errors in Rails, I often want to capture the class name in the log message:
Rails.logger.info("#{self.class.name}.#{__method__} -- info message")
However, unlike an instance method, using self.class.name
in a class method will return "Class" rather than the actual class name. In a class method, use self.name
instead to correctly log the class name. This is because class
is a method that returns the class of your Class
object which is a Class
. Then the name
of a Class
object is just Class
. Hope this makes sense!
Happy coding!
Done
Leave a comment
Comments
Be the first one to leave a comment!