pepa007
2008-May-21 12:31 UTC
list animals with average capture_percent (Josh Susser''s example)
Hi guys, I implemented self-referential system based on Josh Susser''s guidance on 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
AndyV
2008-May-21 15:08 UTC
Re: list animals with average capture_percent (Josh Susser''s example)
"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: <table> <thead> <th><td>Animal</td><td>Captured</td></th> </thead> <tbody> <% @animals.each do |animal_hash| %> <tr><td><%= h animal_hash[:animal].name %></td><td><%animal_hash[:capture_rate].to_s</td></tr> <% end %> </tbody> </table> On May 21, 8:31 am, pepa007 <richter.jo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys, > > I implemented self-referential system based on Josh Susser''s guidance > onhttp://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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---