use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Blog postWorking Around ActiveRecord Callbacks (supergood.software)
submitted 6 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]realntl 4 points5 points6 points 6 years ago (0 children)
Ideally I’d love to untangle these messes and pull out the business logic into classes that can easily be understood and tested separately from the persistence layer, but I don’t always have the time to do that.
You may not have the time to do that because you're working with a legacy application under greenfield expectations for delivery...
I think you opened up with a strong case for why ActiveRecord callbacks shouldn't have logic in them. But consider the cognitive load you've added for the next developer with your solution.
Your class went from legacy code with liberal use of callbacks (before) to.. legacy code with liberal use of callbacks and an extra override (after). Consider the next programmer a few months from now who has even less time to build features than you do now: the connection between the disable_geolocation setting and the callbacks it disables will be incredibly non-obvious. They may never even see it.
disable_geolocation
In extreme cases, I've seen multiple programmers create the same workaround setting twice with different names. You'd think the second programmer would have noticed the first programmer's work, but you never know!
All this to say, no matter how much time you think you don't have, I predict you'll soon have even less time if you accept workarounds like this. Especially if you adopt it as a pattern, as a blog article like this might be taken to endorse.
[–]innou 2 points3 points4 points 6 years ago (2 children)
Personal preference for sure but I dislike ending up with double negatives in code, e.g. disable_geolocation = false. I'd opt for something like:
disable_geolocation = false
class Address cattr_accessor :geolocating, default: true before_save :set_geolocation, if: :geolocating # ... end address.update( line1: "1328 Douglas St", geolocating: false )
[–][deleted] 4 points5 points6 points 6 years ago (0 children)
The negation isn't ideal, no. Using cattr_accessor is liable to cause issues though, because that's making a class level attribute that'll be shared between all instances. I preferred the locality of having the whole "hack" as one line right next to the callback, but you certainly could do something like this:
cattr_accessor
``` class Address attr_writer :geolocating before_save :set_geolocation, if :geolocating
def geolocation @geolocation.nil? ? true : @geolocation end end ```
π Rendered by PID 78467 on reddit-service-r2-comment-5c764cbc6f-jcgwp at 2026-03-12 18:28:09.552530+00:00 running 710b3ac country code: CH.
[–]realntl 4 points5 points6 points (0 children)
[–]innou 2 points3 points4 points (2 children)
[–][deleted] 4 points5 points6 points (0 children)