Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Rails: group by day and one other column
- class Visitor
- has_many :sessions
- end
- class Session
- belongs_to :visitor
- belongs_to :site
- end
- class Site
- has_many :sessions
- end
- visitor_id site_id created_at
- 6 3 2011-09-27
- 6 3 2011-09-27
- 7 3 2011-09-27
- 2 3 2011-09-29
- 7 3 2011-09-29
- Session.group('date(created_at)').group('visitor_id').size
- # => {Tue, 27 Sep 2011=>3, Thu, 29 Sep 2011=>2}
- @counts = Session.group('date(created_at), visitor_id').count
- @counts.each do |(date, visitor), count|
- puts "#{visitor.name} visted the site #{count} times on #{date}"
- end
- Session.where(:created_at => desired_date).group('visitor_id').count
- @counts = Session.group('date(created_at)').
- select("count(distinct sessions.visitor_id) as total, sessions.created_at").
- all
- @counts.each do |count|
- puts "#{count.total} on #{count.created_at.strftime('%m/%d/%y')}"
- end
Add Comment
Please, Sign In to add comment