postgresql - Rails records not displayed when grouped -
i have book model sake of question has 5 fields (id, title, issue, release_date, print_run). i'm trying populate sales table grouped title here's have in controller:
@books = book.all.where("release_date > ?", "2012-05-01").where("print_run > ?", "1").group(:title)
and in view here's part of table:
<% @books.each |title| %> <tr> <td><%= title.title %></td> <td class="text-center"> <% if title.print_run > 1 && title.release_date.between?((date.today - 12.month).beginning_of_month, (date.today - 12.month).end_of_month) %> <%= title.print_run %> <% else %> 0 <% end %> </td> </tr> <% end %>
each book has 1 issue per month have table set 14 columns (title, 1 each month, , total). display print_run of each title month, when use group(:title)
in controller, displays print run last month (but rest 0). if remove group(:title)
controller, displays print runs in correct month has each issue in own run.
here couple of images illustrate issue. in advance!
as fyi reworked whole things...in controller have now:
@book = book.where(:rdate => (date.today - 12.month)..(date.today - 1.month)).where("printrun > ?", "1").select("distinct(title)").group("title").order("title")
...and in view:
<%= book.where(:title => title.title, :rdate => (date.today - 1.month).beginning_of_month..(date.today - 1.month).end_of_month).sum(:printrun) %>
now view part not ideal , i'll move controller @ point, problem solved. note had add select("distinct(title)").group("title") in controller. first part solves unique problem let pgerror because postgresql apparent groups id default. specifying group("title") resolved issue (even if seems apparently redundant).
hope helps next person similar issue.
Comments
Post a Comment