Skip to content

Instantly share code, notes, and snippets.

@timm-oh
Last active March 26, 2024 17:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timm-oh/9b702a15f61a5dd20d5814b607dc411d to your computer and use it in GitHub Desktop.
Save timm-oh/9b702a15f61a5dd20d5814b607dc411d to your computer and use it in GitHub Desktop.
Rails 5.2.3: Monkey patch update_all and update_columns to always include updated_at
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
# Didn't change #update_column because it uses #update_columns under the hood.
def update_columns(attrs)
new_attrs = attrs.symbolize_keys
new_attrs[:updated_at] ||= Time.current if self.class.column_names.include?('updated_at')
super(new_attrs)
end
end
# config/initializers/core_extentions.rb
Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'lib/core_extensions')) + "/**/*.rb"].each do |file|
require file
end
ActiveRecord::Relation.prepend(CoreExtensions::ActiveRecord::CustomUpdateAll)
# lib/core_extensions/active_record/custom_update_all.rb
module CoreExtensions
module ActiveRecord
module CustomUpdateAll
def update_all(updates)
if updates.is_a?(Hash) && !updates.delete(:skip_touch)
# Didn't do updates[:updated_at] ||= Time.current
# because it changes the original updates hash
super({ updated_at: Time.current, **updates.symbolize_keys })
else
super(updates)
end
end
end
end
end
@erados
Copy link

erados commented Aug 3, 2023

Just what I have looked for weeks ๐Ÿ‘

@erados
Copy link

erados commented Aug 3, 2023

Thank you for sharing your code. ๐Ÿ˜€

I have a question, Why did you symbolize updates? Is it necessary ?

@timm-oh
Copy link
Author

timm-oh commented Aug 4, 2023

@erados It's not necessary. Just generally prefer working with symbols everywhere instead of string keys ๐Ÿ˜„

@erados
Copy link

erados commented Nov 11, 2023

Hi @timm-oh .

I hope you're doing well.
I'm reaching out again regarding the article I've been working on for Dev.to, which was inspired by your insightful code.
I'd like to request your permission to reference and discuss your code in the article.
Of course, I'll ensure full credit is given with a direct link to your work.
If you have any concerns or prefer that I don't include it, please let me know, and I'll respect your wishes.

Thanks again for your valuable sharing ๐Ÿ™‚

@timm-oh
Copy link
Author

timm-oh commented Nov 14, 2023

Hey @erados :)

Yeah sure you can use it :)
Side note, @choncou is the original author of this.

@erados
Copy link

erados commented Nov 21, 2023

Thanks! I will mention that too!

@sdhull
Copy link

sdhull commented Feb 27, 2024

Thanks for this nice share ๐Ÿ˜„

You can make requiring core extensions a bit simpler, like this:

Dir[Rails.root.join("lib/core_extensions/**/*.rb")].sort.each do |file|
  require file
end

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