Rails created_at find condition

I am trying to summarize the daily purchase amounts for a given user. @dates is an array of 31 dates. I need a search term to compare a date from an array to the created_at date of purchases. What I am doing below is comparing the exact DateTime value for the create_at column. I need it to look at the day and not the DateTime.

How can I write this so that created_at is between the date from the array?

<% @dates.each do |date| %>
  <td><%= current_user.purchases.sum(:amount, :conditions => ["created_at = ?", date]) %></td>
<% end %>

      

+2


a source to share


2 answers


You need to find the start and end dates in this array, and then you can use records where created_at is in between:



:conditions => ["created_at >= ? AND created_at <= ?", start_date, finish_date]

      

+4


a source


:conditions => {:created_at => start_date..finish_date}

      



+5


a source







All Articles