Skip to content

Instantly share code, notes, and snippets.

@rwilcox
Created February 12, 2011 19:21
Show Gist options
  • Save rwilcox/824021 to your computer and use it in GitHub Desktop.
Save rwilcox/824021 to your computer and use it in GitHub Desktop.
Metaprogramming I did today
# Question: can you tell what's going on here?
classes = subclasses_of(ActiveRecord::Base)
classes.each do |klass|
klass.class_eval do
before_save do |record|
    record.class.columns.each do |c|
      if ( (c.type == :datetime) && (record.send("#{c.name}_changed?")) && record.send("#{c.name}").present? )
        unmunged_datetime = record.send(c.name) + 5.hours
        record.send("#{c.name}=", unmunged_datetime)
      end
    end
end
 end
end
@kunzmann
Copy link

For all active record models, save time + 5.hours if you happen to be updating any datetime columns...

Though, at first glance it seems like it may not be particularly performance friendly & could clobber existing before_save functionality?

@rwilcox
Copy link
Author

rwilcox commented Feb 16, 2011

Exactly. :)

That's an interesting thought RE performance (we don't have enough data to really have a hit). And umm I didn't think about clobbering existing before_save filters (in our case it wasn't an issue, but very good point)

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