Class: Analytic::BudgetDimension

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/analytic/budget_dimension.rb

Overview

== Schema Information

Table name: analytic_budget_dimensions
Database name: primary

id :integer not null, primary key
account_description :string
children_count :integer
description :string
ledger_account_ids :string default([]), is an Array
ledger_company_account_ids :text default([]), is an Array
created_at :datetime not null
updated_at :datetime not null
budget_group_id :integer
business_unit_id :integer
company_id :string
ledger_account_id :integer
ledger_project_id :integer
parent_id :integer
supplier_id :integer

Indexes

by_cid_laid_bgid_w (company_id,ledger_account_ids,budget_group_id) WHERE ((business_unit_id IS NULL) AND (ledger_project_id IS NULL) AND (supplier_id IS NULL) AND (parent_id IS NULL))
by_cid_laid_buid_lpid_bgid_pid_w (company_id,ledger_account_ids,business_unit_id,ledger_project_id,budget_group_id,parent_id) WHERE (supplier_id IS NULL)
idx_budget_dimensions_company_account_bu_project_supplier (company_id,ledger_account_id,business_unit_id,ledger_project_id,supplier_id)
idx_budget_dimensions_structural_unique (company_id,ledger_account_ids,business_unit_id,ledger_project_id,supplier_id,budget_group_id,parent_id) UNIQUE NULLS NOT DISTINCT
index_analytic_budget_dimensions_on_parent_id (parent_id)

Has many collapse

Class Method Summary collapse

Class Method Details

.find_or_create_with(existing_attrs, new_attrs) ⇒ Analytic::BudgetDimension

Find the dimension identified by +existing_attrs+, or create it, applying
+new_attrs+ either way.

Atomic via +create_or_find_by+ against the unique index on the structural key
(+idx_budget_dimensions_structural_unique+): the INSERT is attempted first and a
losing race surfaces as +ActiveRecord::RecordNotUnique+, which +create_or_find_by+
rescues and resolves to the winning row. This replaces the session-scoped
advisory lock added in PR #1346 — concurrent Analytic::BudgetFact::Refresher
runs (BudgetRefresherAllWorker fans 12 monthly jobs onto the concurrency-16
:budgets queue) can no longer insert duplicate rows for the same key.

+new_attrs+ are applied on create (via the block, before the INSERT) and on
find (the trailing +update!+), preserving the previous find-or-create-then-update
behaviour.

Parameters:

  • existing_attrs (Hash)

    structural attributes identifying the dimension

  • new_attrs (Hash)

    mutable attributes to set on find or create

Returns:

  • (Analytic::BudgetDimension)


80
81
82
83
84
# File 'app/models/analytic/budget_dimension.rb', line 80

def self.find_or_create_with(existing_attrs, new_attrs)
  dimension = create_or_find_by(existing_attrs) { |d| d.assign_attributes(new_attrs) }
  dimension.update!(new_attrs) unless dimension.previously_new_record?
  dimension
end

Instance Method Details

#budget_factsActiveRecord::Relation<Analytic::BudgetFact>

Returns:



40
# File 'app/models/analytic/budget_dimension.rb', line 40

has_many :budget_facts