Last active
December 21, 2015 05:08
-
-
Save bensheldon/6254499 to your computer and use it in GitHub Desktop.
Using serializable hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Organization | |
include ActiveModel::Serialization | |
has_many :grants, as: :grants_made | |
has_many :grants, as: :grants_received | |
def grants_received_total | |
grants_received.map(&:amount).inject(0, :+) # or .sum if you're all into ActiveSupport | |
end | |
def grants_made_total | |
grants_made.map(&:amount).inject(0, :+) | |
end | |
def serializable_hash(options={}) | |
defaults = { | |
methods: [:grants_received_total, :grants_made_total] | |
} | |
super options.merge(defaults) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment