janida janida - 19 days ago 7
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

Name
id int
date date
item_id int


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

@names = Name.find(:all)
@list=Array.new
@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")
end


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

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