I have a method which goes through each day of the week:
def dates_week(d, delim)
"<tr>" + (d.beginning_of_week...(d.beginning_of_week+5)).map do |day|
"<#{delim}> #{yield(day)} </#{delim}>"
end.join + "</tr>"
end
For each day of the week, I plug that as an arg into a method (or maybe a named_scope, haven't figured out which), that will then output the .count for :all the emails that have a :date_sent on that date. However, :date_sent is a date-timestamp, so I can't use == as I have below.
def sent_emails_by_date(date)
ContactEmail.find(:all, :conditions => "date_sent = '#{date}'"
" ).count
end
开发者_开发百科
For example, in the View this is what I use:
<table class='reports'>
<%= dates_week(Date.today, "th") {|d| d.strftime("%A")} %>
<%= dates_week(Date.today, "td") {|d| sent_emails_by_date(d)}%>
</table>
How do I find all the emails that fall on the day for the date passed through from the method which loops through a week as shown above?
Try
def send_emails_by_date(date)
ContactEmail.find(:all, :conditions => { :date_sent => (date.at_beginning_of_day)...(date.end_of_day)).count
end
精彩评论