Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert how to get missing months...

Replytotopic

how to get missing months

Posted in Forums : Ask a Rails expert

 
Profile

Authority 25
Posting Rating 0
Sign in to rate this post

Hi,
i am using a Rails count method like this :
start_date = Date.civil(y=2008,m=1)
@users_count = User.count(:all, :conditions => [“last_updated BETWEEN ? and ?”, start_date.>>(Date.today.month – 8), Date.today], :group => “DATE_FORMAT(last_updated, ‘%b %Y’)”, :order => :last_updated)

getting output like this

["Feb 2008", 60], ["Mar 2008", 70], ["May 2008", 46], ["Jun 2008", 33], ["Aug 2008", 10]]

but i was missing the months Apr,Jul when there were no records in that months
Can we get the 2 months when there was no data in that months as [“Apr 2008”, nil], [“Jul 2008”, nil] or in any way ?

any help ??

thanks

 
N739949849_5167

Authority 12
Posting Rating 96
Sign in to rate this post

You have two approaches there: the first is by appending the missing dates in Ruby (you may want to override the Array class for that), and the second one is by creating a year_months table and using it as a left join in your call to User.count.

Your year_months table should look like this:

id
year_month

Where year_month contains the Month/Year combinations, i.e., “Jan 2008” to “Dec 2008”.

EDIT: You can get rid of the id field if you have no need for it.

Replytotopic

Other Recent Topics

Ask a Rails expert : calling functions from model to controller

Ask a Rails expert : calling functions from model to controller

Ask a Rails expert : rspec examples?

Ask a Rails expert : Radio buttons with Observe field

Ask a Rails expert : copy to clipboard

Ask a Rails expert : url_for

Ask a Rails expert : API call problem

Ask a Rails expert : Invoke a plugin from a plugin?

Ask a Rails expert : Restful problem

Ask a Rails expert : using $F

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel