postgresql - Ruby on Rails group -
i using postgresql in rails application , have following model
log(id: integer, session: string, user: string, application: string, activity: string, event: string, time: datetime, parameters: hstore, extras: hstore, created_at: datetime, updated_at: datetime)
now, count of event each user. type following in rails console
log.select("count(event) event_count").group("user")
but , get
log load (0.7ms) select count(event) event_count "logs" group user => #<activerecord::relation [#<log id: nil>]>
am doing terribly wrong?
to statistics every user use group , count this:
log.group('"user"').count(:event)
this return hash having user key , value count value this:
{name1: 2, name2: 12, ... }
the query sent database:
select count("logs"."event") count_event, "user" user "logs" group "user"
i double escaped user
column let postgres use column named user logs table , not user function defined postgres.
the way you're doing not wrong, more verbose. see result when
log.select("count(event) event_count").group("user")
do this:
log.select('"user", count(event) event_count').group('"user"').all.each |stats| puts "#{stats.user} => #{stats.event_count}" end
Comments
Post a Comment