Skip to content

Instantly share code, notes, and snippets.

@hampelm
Forked from bensheldon/organization.rb
Created August 17, 2013 16:28
Show Gist options
  • Save hampelm/6257684 to your computer and use it in GitHub Desktop.
Save hampelm/6257684 to your computer and use it in GitHub Desktop.
class Organization < ActiveRecord::Base
include ActiveModel::Serialization
extend FriendlyId
attr_accessible :description, :name, :slug, :type, :grants_received, :grants_made
friendly_id :name, use: :slugged
has_many :grants_received, :class_name => 'Grant', :foreign_key => 'to_id'
has_many :grants_made, :class_name => 'Grant', :foreign_key => 'from_id'
validates :name, :uniqueness => true
def total_grants_received
grants_received.sum(:amount)
end
def total_grants_made
grants_made.sum(:amount)
end
def serializable_hash(options={})
super.serializable_hash options.merge(methods: [:total_grants_received, :total_grants_made])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment