ruby on rails - Counting associated objects from an array of parent objects -
i working report counts neighbors in household - looking @ using either helper method, simple query or both
a household object has several neighbors , want count total number of neighbors in select group of households. have array of households ids:
@household_ids = [31, 15, 30, 38, 1, 5, 32, 25, 10, 26, 14,29]
i tried this:
def household_neighbor_count(houses) houses.each |id| @neigh = household.find(id).neighbor_count @neigh end end
which doesn't work - returns list of ids
since rails activerecord query , shot in pseudo sql:
neighbors household_id == household_id in @household-ids
i using squeel if helps
how either -approach fine or recommendation of best approach great
it unclear trying do, guess want:
houses.map{|id| household.find(id).neighbor_count}.inject(:+)
if there duplicates within neighbors among houses, need method set of neighbors given house, not count. , since have not shown such method, guess either not issue, or question inappropriate.
Comments
Post a Comment