# List animals with average capture\_percent (Josh Susser's example)

**URL:** <https://www.ruby-forum.com/t/list-animals-with-average-capture-percent-josh-sussers-example/138263>\
**Category:** Rails\
**Created:** [May 21, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/list-animals-with-average-capture-percent-josh-sussers-example/138263 "2008-05-21T14:38:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 21, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/list-animals-with-average-capture-percent-josh-sussers-example/138263/1 "2008-05-21T14:38:43Z")

</div>

Hi guys,

I implemented self-referential system based on Josh S.'s guidance  
on [http://blog.hasmanythrough.com/2007/10/](http://blog.hasmanythrough.com/2007/10/) … ny-through

now I’m unsuccesfully trying to list all animals together with average  
hunt.capture\_percent, regardless of whether it is ‘pursuit’ or  
‘escape’ (i am using it on sthg else than animals where it makes sense  
not to differentiate between these. i want to keep having separated  
‘pursuit’ and ‘escape’ for future though)

i tried sthg like

\<%= (animal.pursuit.average(:capture\_percent)  
+animal.pursuit.average(:capture\_percent))/2 %\>

first, this doesn’t work, while having just \<%=  
(animal.pursuit.average(:capture\_percent) %\> does

second, i believe this calculation should rather be included in  
controller, but i have no idea how to do it over an array, join this  
array with the array of @animals and send this all to view ☹

this might be rather stupid newbie question, but I failed to google  
out an answer ☹ thanks for any trace of help

---

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 21, 2008, 5:09pm UTC](https://www.ruby-forum.com/t/list-animals-with-average-capture-percent-josh-sussers-example/138263/2 "2008-05-21T17:09:00Z")

</div>

“This doesn’t work” won’t get much help. In what way does it fail?

As for the second part, I’d suggest using map/collect from Enumerable  
to marry these in the controller if you want to move the calculation  
there. The approach really depends on what you need/want to do in the  
view. You might want to return the animal object, just it’s name,  
etc. but it’d look something like this:

@animals = Animal.whatever\_you\_do\_to\_find\_animals.collect{|animal|  
{:animal=\>animal,  
:capture\_rate=\>animal.pursuit.average(:capture\_percent)}}

Then in your view:

 \<% @animals.each do |animal\_hash| %\> \<% end %\> 

| | Animal | Captured |
| \<%= h animal\_hash[:animal].name %\> | \<%= animal\_hash[:capture\_rate].to\_s |
