# Calculations on nested resources

**URL:** <https://www.ruby-forum.com/t/calculations-on-nested-resources/142880>\
**Category:** Rails\
**Created:** [July 12, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/calculations-on-nested-resources/142880 "2008-07-12T16:16:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 12, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/calculations-on-nested-resources/142880/1 "2008-07-12T16:16:30Z")

</div>

Hi,

I can’t seem to find a solution to the folowing problem.  
I have 3 resources that are all bound to eachother like this:

class Item \< ActiveRecord::Base  
belongs\_to :item\_category  
// has a date and amount attr  
end

class ItemCategory \< ActiveRecord::Base  
has\_many :items  
has\_many :item\_tops  
// groups the items in categories  
end

class ItemTop \< ActiveRecord::Base  
belongs\_to :item\_category  
// has a start and stop date and an amount attr  
end

The thing I would like to do now starts from the ItemTop resource. When  
I do a search, it will search for all ItemTop resources where the start  
and stop dates are between or equal to 2 dates the user inserted in the  
search box.

The result of the search must contain the following data:  
the ItemTop amount and the ItemCategory it reflects upon (this I have so  
far).

But next, it must show the total sum of the amounts of the Item  
resources where the Item.date also resigns between the 2 dates the user  
added. I have no idea how I can get this implemented in clean Ruby  
style.

I hope someone can help me out with this.

Thank you in advance

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 12, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/calculations-on-nested-resources/142880/2 "2008-07-12T19:55:14Z")

</div>

I don’t want to come over like a lazy person that just want someone else  
to write the code for him. I know a way, but it is not the best idea due  
to performance issues.

The way I’m working now it to define the following method in my model:

def actual  
total = BigDecimal.new(“0”)  
item\_category.items.each do |set|  
if set.set\_date \>= start\_date && set.set\_date \<= stop\_date  
total += set.amount  
end  
end  
return total  
end

It gets the job done, but it iterates over every item. Is there a way to  
solve this by just retrieve the good items from the database instead of  
all the items?

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 12, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/calculations-on-nested-resources/142880/3 "2008-07-12T22:08:59Z")

</div>

On 12 Jul 2008, at 18:55, Michael R. wrote:

> total = BigDecimal.new(“0”)  
> item\_category.items.each do |set|  
> if set.set\_date \>= start\_date && set.set\_date \<= stop\_date  
> total += set.amount  
> end  
> end

Something like  
item\_category.items.sum :amount, :conditions =\> …  
?  
Fred

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [July 12, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/calculations-on-nested-resources/142880/4 "2008-07-12T22:23:56Z")

</div>

Frederick C. wrote:

> On 12 Jul 2008, at 18:55, Michael R. wrote:
> 
> > total = BigDecimal.new(“0”)  
> > item\_category.items.each do |set|  
> > if set.set\_date \>= start\_date && set.set\_date \<= stop\_date  
> > total += set.amount  
> > end  
> > end
> 
> Something like  
> item\_category.items.sum :amount, :conditions =\> …  
> ?  
> Fred

Well I’ll be damned Fred. I didn’t know that this was even possible in  
Rails.  
Looks like I need more reading 🙂

Thank you for the help
