Have you though about using a scope and sql to do all this for you? I do something similar but not quite as complex but I would thing you could use sql to aggregate the numbers you need.
Here is where I find all materials that are loaded for a building. This is in the materials model and I pass to it in controller where a need a collection of all building materials the building I'm interested in ie @building_materials = Material.for_building(@material.building)
scope :for_building, lambda { |b|
where("building_id = ?",
b.id).
order(:name)
}
Bob