Rails find with conditional dates

To use the rails find method with conditional dates do the following. inspiration from here

Example:

            Record.find :all, :conditions => {:date => 20070400..20070431, :user_id => current_user.id}

Or addition: Record.sum :hours, :conditions => {:date => 20070400..20070431, :user_id => current_user.id}

Now what if you want to use the current month instead of the static typed in months above?

             this_month = Date.today.strftime("%Y%m")
start_of_month = this_month + '00'
end_of_month = this_month + '31'
result = Record.sum :hours, :conditions => {:date => start_of_month..end_of_month, :user_id => current_user.id} rescue nil
result = result.round(1) rescue nil

Rails Date method:

Comments