class LndhubAccount < LndhubBase self.table_name = "accounts" self.inheritance_column = :_type_disabled has_many :ledgers, class_name: "LndhubAccountLedger", foreign_key: "account_id" belongs_to :user, class_name: "LndhubUser", foreign_key: "user_id" scope :current, -> { where(type: "current") } scope :outgoing, -> { where(type: "outgoing") } scope :incoming, -> { where(type: "incoming") } scope :fees, -> { where(type: "fees") } scope :with_balances, -> { current.joins(:user).joins(:ledgers) .group("accounts.id", "users.login") .select("accounts.id, users.login, SUM(account_ledgers.amount) AS balance") } end