Skip to content

Instantly share code, notes, and snippets.

@michiels
Last active December 26, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michiels/7155889 to your computer and use it in GitHub Desktop.
Save michiels/7155889 to your computer and use it in GitHub Desktop.
class Organization
has_many :organization_histories
history_methods = [
:coc_number,
:coc_office
]
history_methods.each do |method|
define_method(method) do
if self.organization_histories.any?
# Select the last known organization_history object from the DB
self.organization_histories.order("active_from DESC").limit(1).first.call(method)
# No related organization_history models. Get the coc_number from the organization model
else
read_attribute(method)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment