Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated count in rails 4

a = Session.where(:date_at => date_from..date_to).
            order("date_at").
            count("DISTINCT(user_id)",
                  :group => "date(convert_tz(date_at, 'UTC', 'Europe/Warsaw'))")

How can I write this in Rails 4 ?

This generates error:

DEPRECATION WARNING: Relation#calculate with finder options is deprecated. Please build a scope and then call calculate on it instead. (called from block (2 levels) in <top (required)> at /Users/xxx/Desktop/DEPLOY/d1_mysql/d1/app/admin/user.rb:11)
DEPRECATION WARNING: The :distinct option for `Relation#count` is deprecated. Please use `Relation#distinct` instead. (eg. `relation.distinct.count`). (called from block (2 levels) in <top (required)> at /Users/xxx/Desktop/DEPLOY/d1_mysql/d1/app/admin/user.rb:11)
like image 300
Wordica Avatar asked Aug 28 '13 12:08

Wordica


2 Answers

More rails 4 way . .

Session.where(date_at: date_from..date_to).select('distinct user_id')
       .group("date(convert_tz(date_at, 'UTC', 'Europe/Warsaw'))").count
like image 52
Thaha kp Avatar answered Nov 04 '22 04:11

Thaha kp


You can try with this:

Session.where(:date_at => date_from..date_to)
       .select('user_id').distinct
       .group("date(convert_tz(date_at, 'UTC', 'Europe/Warsaw'))")
       .count
like image 24
jurglic Avatar answered Nov 04 '22 05:11

jurglic