Skip to content

Instantly share code, notes, and snippets.

@crispincornett
Last active December 21, 2015 04:58
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 crispincornett/6253344 to your computer and use it in GitHub Desktop.
Save crispincornett/6253344 to your computer and use it in GitHub Desktop.
Polymorphic Invoice Charges
class Invoice < ActiveRecord::Base
belongs_to :account
has_many :charges
end
class Charge < ActiveRecord::Base
belongs_to :invoice
belongs_to :chargeable, :polymorphic => true
delegate :total, to: :chargeable, prefix: true
scope :ordered_by_date, -> { order(:created_at) }
def self.line_items_only
where(:chargeable_type => "LineItem")
end
def self.line_items
LineItem.where(:id => line_items_only.pluck(:chargeable_id))
end
def self.interest_charges_only
where(:chargeable_type => "InterestCharge")
end
def self.interest_charges
InterestCharge.where(:id => interest_charges_only.pluck(:chargeable_id))
end
def self.adjustments_only
where(:chargeable_type => "Adjustment")
end
def self.adjustments
Adjustment.where(:id => adjustments_only.pluck(:chargeable_id))
end
end
class LineItem < ActiveRecord::Base
has_one :charge, :as => :chargeable
end
class InterestCharge < ActiveRecord::Base
has_one :charge, :as => :chargeable
end
class Adjustment < ActiveRecord::Base
has_one :charge, :as => :chargeable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment