Answer the question
In order to leave comments, you need to log in
Ruby on Rails 4. How to write a query to group rows by date?
Hello. I can't write the right query to get this result (group records by day):
date(day) - sum(field1) - sum(field2)
@downloads = Download
.order(created_at: :desc)
.includes(:document)
.joins(:document)
.where('documents.uploaded_by = ?', params[:user_id])
.page(params[:page])
Answer the question
In order to leave comments, you need to log in
The general principle is this:
Download
.joins(:document)
.where(documents: {uploaded_by: params[:user_id]})
.group(:created_at)
.pluck(:created_at, 'sum(поле1)', 'sum(поле2)')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question