janida janida - 11 months ago 62
Ruby Question

my loop skip empty data

I have a loop problem.
I want to collect name counts for each items for every months
This code skips variables with empty value, but I needs those values.
below is a bit of the tables involve

id int
date date
item_id int

so I need to loop them and put it in an array.

@names = Name.find(:all)
@names.each do |e|
@list << Name.count(:all,:conditions=>["item_id = ? and DATE FORMAT(date,%M) = ?",e.item_id,date],:group =>DATE FORMAT(date,%m),:order=>"date ASC")

so, for every item_id which have empty count, it did not go into the array.
how do I include them?

Answer Source
@list << Name.count(
  :conditions=>["item_id = ? and DATE FORMAT(date,%M) = ?",e.item_id,date],
  :group =>DATE FORMAT(date,%m),
  :order=>"date ASC"
) || nil