V
V
Vitaly Igorevich2015-07-14 18:27:39
Ruby on Rails
Vitaly Igorevich, 2015-07-14 18:27:39

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)

The code that is:
@downloads = Download
.order(created_at: :desc)
.includes(:document)
.joins(:document)
.where('documents.uploaded_by = ?', params[:user_id])
.page(params[:page])

document has_many downloads
download belongs_to
document

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vsuhachev, 2015-07-15
@dev1vitaly

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)')

S
Sergey, 2015-07-15
@mastedm

There is such a useful gem for grouping by dates, but it won't do '-sum...' for you.
It seems to me that it's easier to write a query in pieces of SQL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question