So frequently I find myself writing code like this:
song.rb:
:before_save :cache_sortable_name
private
def cache_sortable_name
return unless name_changed?
self.sortable_name = name.sub(/^(the|a|an)\s+/i, '')
end
I.e., I have a sortable_name
database column which holds denormalized data for convenience, and I want to populate it whenever the model's name changes.
I would like to be able to encapsulate this logic in a construct such as this
:cache_in_database :sortable_name do
name.sub(/^(the|a|an)\s+/i, '')
end
or something. Does this exist?