Overriding methods as a Rubyist is extremely natural and powerful. Recently, I did run into a circumstance where I had an ActiveRecord object with a DateTime that I needed in a specific TimeZone (based on user input). If you ever have tried to override the getter for an attribute with the same name and have been stumped, this article details one way of making it work.
Alright, first off some example data to support our solution.
With the above, anytime I wanted to output the Event’s
start_time to the front-end I would need to reach
Returns the simultaneous time in Time.zone, or the specified zone. - apidock.com, Ruby on Rails
Here’s an example of outputting the Event object’s
start_time in the proper
Quite, verbose no?
Well what if we just override the
Event#start_time getter to already
perform the conversion?
Nope! This will result in the dreaded
SystemStackError: stack level too deep, which basically translated to not having a proper stop condition in a recursion loop. Or in our case overriding a method and using the same method name within it.
But all hope is not lost.
read_attribute to the rescue!
read_attribute allows you to return the value from ActiveRecord before it makes it to the getter method. Perfect! Here’s the new working solution.
We’ve now successfully overridden a named attribute using the value from the same attribute. Neat!
Got any other Ruby method tricks? Is there a better way to accomplish the above? Leave me a comment below.comments powered by Disqus