def apply_to_donation(donation) if active? amount_to_apply = donation.project_amount donation.grant_amount ||= 0 if remaining_amount >= amount_to_apply # Case: There is enough remaining to match the gift donation.grant_amount += amount_to_apply self.remaining_amount -= amount_to_apply else # Case: There is NOT enough remaining to match the gift donation.grant_amount += amount_remaining self.remaining_amount -= amount_remaining end donation.grants.push(self) donation.save! self.save! end end