Guest User

Untitled

a guest
Aug 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. Rails: group by day and one other column
  2. class Visitor
  3. has_many :sessions
  4. end
  5.  
  6. class Session
  7. belongs_to :visitor
  8. belongs_to :site
  9. end
  10.  
  11. class Site
  12. has_many :sessions
  13. end
  14.  
  15. visitor_id site_id created_at
  16. 6 3 2011-09-27
  17. 6 3 2011-09-27
  18. 7 3 2011-09-27
  19. 2 3 2011-09-29
  20. 7 3 2011-09-29
  21.  
  22. Session.group('date(created_at)').group('visitor_id').size
  23.  
  24. # => {Tue, 27 Sep 2011=>3, Thu, 29 Sep 2011=>2}
  25.  
  26. @counts = Session.group('date(created_at), visitor_id').count
  27.  
  28. @counts.each do |(date, visitor), count|
  29. puts "#{visitor.name} visted the site #{count} times on #{date}"
  30. end
  31.  
  32. Session.where(:created_at => desired_date).group('visitor_id').count
  33.  
  34. @counts = Session.group('date(created_at)').
  35. select("count(distinct sessions.visitor_id) as total, sessions.created_at").
  36. all
  37.  
  38. @counts.each do |count|
  39. puts "#{count.total} on #{count.created_at.strftime('%m/%d/%y')}"
  40. end
Add Comment
Please, Sign In to add comment