Hi everybody! I'm junior rails programmer. I have some optimization questions about eager loading and Models at all.
So I have such relationships:
Program --< Subprograms --< Events --< Financings
\--<Events --< Financings
Program and subprogram is self-referrental sti models. I need to have a bunch of virtual fields in events, subprograms and programs that reflects their financings. So here I show the code and want to receve from people it's review - all is matter (style, functionality, "way") This is the Program:
class Dcp < Program
has_many :subprograms,
:class_name => 'DcpSubprogram',
:foreign_key => 'parent_id'
has_many :events,
:foreign_key => 'dcp_id'
has_many :building_objects,
:foreign_key => 'dcp_id'
has_many :other_investments,
:foreign_key => 'dcp_id'
has_many :niokrs,
:foreign_key => 'dcp_id'
has_many :other_costs,
:foreign_key => 'dcp_id'
include ProgramFinancing
end
This is the Subprogram:
class DcpSubprogram < Program
belongs_to :dcp,
:class_name => 'Dcp',
:foreign_key => 'parent_id'
has_many :events,
:foreign_key => 'dcp_id'
has_many :building_objects,
:foreign_key => 'dcp_id'
has_many :other_investments,
:foreign_key => 'dcp_id'
has_many :niokrs,
:foreign_key => 'dcp_id'
has_many :other_costs,
:foreign_key => 'dcp_id'
include SubprogramFinancing
end
This is Libs for them:
module ProgramFinancing
def self.included(base)
base.class_eval do
def after_initialize
@all_subprograms = subprograms.all
@all_events = events.all
end
def actual_federal_financing
@all_subprograms.sum(&:actual_federal_financing) +
@all_events.sum(&:actual_federal_financing)
end
def actual_current_regional_financing
@all_subprograms.sum(&:actual_current_regional_financing) +
@all_events.sum(&:actual_current_regional_financing)
end
def actual_old_regional_financing
@all_subprograms.sum(&:actual_old_regional_financing) +
@all_events.sum(&:actual_old_regional_financing)
end
def actual_regional_financing
actual_current_regional_financing + actual_old_regional_financing
end
def actual_local_financing
@all_subprograms.sum(&:actual_current_regional_financing) +
@all_events.sum(&:actual_current_regional_financing)
end
def actual_extrabudgetary
@all_subprograms.sum(&:actual_local_financing) +
@all_events.sum(&:actual_local_financing)
end
def flow_federal_financing
@all_subprograms.sum(&:flow_federal_financing) +
@all_events.sum(&:flow_federal_financing)
end
def flow_current_regional_financing
@all_subprograms.sum(&:flow_current_regional_financing) +
@all_events.sum(&:flow_current_regional_financing)
end
def flow_old_regional_financing
@all_subprograms.sum(&:flow_old_regional_financing) +
@all_events.sum(&:flow_old_regional_financing)
end
def flow_regional_financing
@all_subprograms.sum(&:flow_regional_financing) +
@all_events.sum(&:flow_regional_financing)
end
def flow_local_financing
@all_subprograms.sum(&:flow_local_financing) +
@all_events.sum(&:flow_local_financing)
end
def flow_extrabudgetary
@all_subprograms.sum(&:flow_extrabudgetary) +
@all_events.sum(&:flow_extrabudgetary)
end
def planned_federal_financing
@all_subprograms.sum(&:planned_federal_financing) +
@all_events.sum(&:planned_federal_financing)
end
def planned_current_regional_financing
@all_subprograms.sum(&:planned_current_regional_financing) +
@all_events.sum(&:planned_current_regional_financing)
end
def planned_old_regional_financing
@all_subprograms.sum(&:planned_old_regional_financing) +
@all_events.sum(&:planned_old_regional_financing)
end
def planned_regional_financing
planned_current_regional_financing + planned_old_regional_financing
end
def planned_local_financing
@all_subprograms.sum(&:planned_local_financing) +
@all_events.sum(&:planned_local_financing)
end
def planned_extrabudgetary
@all_subprograms.sum(&:planned_extrabudgetary) +
@all_events.sum(&:planned_extrabudgetary)
end
def total_actual_financing
actual_federal_financing +
actual_regional_financing +
actual_local_financing +
actual_extrabudgetary
end
def total_flow_financing
flow_federal_financing +
flow_regional_financing +
flow_local_financing +
flow_extrabudgetary
end
def total_planned_financing
planned_federal_financing +
planned_regional_financing +
planned_local_financing +
planned_extrabudgetary
end
end
end
end
yet one:
module SubprogramFinancing
def self.included(base)
base.class_eval do
def after_initialize
@all_events = events.all
end
def actual_federal_financing
@all_events.sum(&:actual_federal_financing) || 0
end
def actual_current_regional_financing
@all_events.sum(&:actual_current_regional_financing) || 0
end
def actual_old_regional_financing
@all_events.sum(&:actual_old_regional_financing) || 0
end
def actual_regional_financing
actual_current_regional_financing + actual_old_regional_financing
end
def actual_local_financing
@all_events.sum(&:actual_current_regional_financing) || 0
end
def actual_extrabudgetary
@all_events.sum(&:actual_local_financing) || 0
end
def flow_federal_financing
@all_events.sum(&:flow_federal_financing) || 0
end
def flow_current_regional_financing
@all_events.sum(&:flow_current_regional_financing) || 0
end
def flow_old_regional_financing
@all_events.sum(&:flow_old_regional_financing) || 0
end
def flow_regional_financing
@all_events.sum(&:flow_regional_financing) || 0
end
def flow_local_financing
@all_events.sum(&:flow_local_financing) || 0
end
def flow_extrabudgetary
@all_events.sum(&:flow_extrabudgetary) || 0
end
def planned_federal_financing
@all_events.sum(&:planned_federal_financing) || 0
end
def planned_current_regional_financing
@all_events.sum(&:planned_current_regional_financing) || 0
end
def planned_old_regional_financing
@all_events.sum(&:planned_old_regional_financing) || 0
end
def planned_regional_financing
planned_current_regional_financing + planned_old_regional_financing
end
def planned_local_financing
@all_events.sum(&:planned_local_financing) || 0
end
def planned_extrabudgetary
@all_events.sum(&:planned_extrabudgetary) || 0
end
def total_actual_financing
actual_federal_financing +
actual_regional_financing +
actual_local_financing +
actual_extrabudgetary
end
def total_flow_financing
flow_federal_financing +
flow_regional_financing +
flow_local_financing +
flow_extrabudgetary
end
def total_planned_financing
planned_federal_financing +
planned_regional_financing +
planned_local_financing +
planned_extrabudgetary
end
end
end
end
Event code:
class Event < ActiveRecord::Base
belongs_to :dcp,
:class_name => 'Dcp',
:foreign_key => 'dcp_id'
belongs_to :fcp,
:class_name => 'Fcp',
:foreign_key => 'fcp_id'
belongs_to :dcp_subprogram,
:class_name => 'DcpSubprogram',
:foreign_key => 'dcp_id'
belongs_to :fcp_subprogram,
:class_name => 'FcpSubprogram',
:foreign_key => 'fcp_id'
has_many :actual_financings
has_many :planned_financings
has_many :flow_financings
has_many :planned_results
has_many :achieved_results
def after_initialize
@actual_financing = actual_financings.first(:order => 'date DESC',
:include => :district_financings)
@flow_financing = flow_financings.first(:order => 'date DESC',
:include => :district_financings)
@planned_financing = planned_financings.first(:order => 'date DESC',
:include => :district_financings)
end
def actual_federal_financing
@actual_financing.try(:federal_budget) || 0
end
def actual_current_regional_financing
@actual_financing.try(:current_regional_budget) || 0
end
def actual_old_regional_financing
@actual_financing.try(:old_regional_budget) || 0
end
def actual_regional_financing
actual_current_regional_financing + actual_old_regional_financing
end
def actual_local_financing
@actual_financing.try(:local_budget) || 0
end
def actual_extrabudgetary
@actual_financing.try(:extrabudgetary) || 0
end
def flow_federal_financing
@flow_financing.try(:federal_budget) || 0
end
def flow_current_regional_financing
@flow_financing.try(:current_regional_budget) || 0
end
def flow_old_regional_financing
@flow_financing.try(:old_regional_budget) || 0
end
def flow_regional_financing
flow_current_regional_financing + flow_old_regional_financing
end
def flow_local_financing
@flow_financing.try(:local_budget) || 0
end
def flow_extrabudgetary
@flow_financing.try(:extrabudgetary) || 0
end
def planned_federal_financing
@planned_financing.try(:federal_budget) || 0
end
def planned_current_regional_financing
@planned_financing.try(:current_regional_budget) || 0
end
def planned_old_regional_financing
@planned_financing.try(:old_regional_budget) || 0
end
def planned_regional_financing
planned_current_regional_financing + planned_old_regional_financing
end
def planned_local_financing
@planned_financing.try(:local_budget) || 0
end
def planned_extrabudgetary
@planned_financing.try(:extrabudgetary) || 0
end
def total_actual_financing
actual_federal_financing +
actual_regional_financing +
actual_local_financing +
actual_extrabudgetary
end
def total_flow_financing
flow_federal_financing +
flow_regional_financing +
flow_local_financing +
flow_extrabudgetary
end
def total_planned_financing
planned_federal_financing +
planned_regional_financing +
planned_local_financing +
planned_extrabudgetary
end
end
Financing:
class Financing < ActiveRecord::Base
validates_numericality_of :federal_budget, :current_regional_budget,
:old_regional_budget, :local_budget, :extrabudgetary
def after_initialize
if (self.is_a?(ActualFinancing) || self.is_a?(FlowFinancing) || self.is_a?(PlannedFinancing))
unless (@district_financings = district_financings).empty?
def self.federal_budget
@district_financings.sum('federal_budget')
end
def self.current_regional_budget
@district_financings.sum('current_regional_budget')
end
def self.old_regional_budget
@district_financings.sum('old_regional_budget')
end
def self.local_budget
@district_financings.sum('local_budget')
end
def self.extrabudgetary
@district_financings.sum('extrabudgetary')
end
end
end
end
def regional_budget
current_regional_budget + old_regional_budget
end
def total_budget
federal_budget + regional_budget + local_budget + extrabudgetary
end
end
With financing there is an organisation problem. There can be yet one level deeper (DistrictFinancing) but this is not necessary. So I saw many books but they are all about Rails features but there is few answers on how to use this features correctly. So any advice will be valuable for me.