mysql - Rails Active record doesnt have X as Y field -


rails beginner, query works on mysql -- select x y doesnt seem working on rails's query.

count of field m1 "cnt" ==> active record doesnt exist ( query given works on mysql )

ab.where("m = 1").group("unix_timestamp(m_timestamp) div 6048000 ").select("m_timestamp, count(m1) cnt") ab load (3.5ms)  select m_timestamp, count(m1) cnt `a_b` (m = 1) group unix_timestamp(m_timestamp) div 6048000  => #<activerecord::relation [#<ab id: nil, m_timestamp: "2014-08-01 22:21:09">, #<ab id: nil, m_timestamp: "2014-10-05 00:15:23">, #<ab id: nil, m_timestamp: "2014-12-20 11:55:45">, #<ab id: nil, m_timestamp: "2015-02-04 23:45:29">, #<ab id: nil, m_timestamp: "2015-04-02 03:51:14">, #<ab id: nil, m_timestamp: "2015-06-19 22:25:06">]>      

the above mysql query -- select m_timestamp, count(m1) cnt 'a_b' (m = 1) group unix_timestamp(m_timestamp) div 6048000 works in mysql.

if use count, i'll grouped counts key - i'm not sure is.

ab.where("m = 1").group("unix_timestamp(m_timestamp) div 6048000 ").count {232=>72, 233=>69, 234=>73, 235=>76, 236=>58, 237=>15} 

i want m_timestamp key of above result. how can achieve this?

sample output of mysql result as:

mysql> select m_timestamp, count(user_id) cnt `a_b` (m = 1) group unix_timestamp(m_timestamp) div 6048000  order `a_b`.`m_timestamp` asc; +---------------------+-----+ | m_timestamp         | cnt | +---------------------+-----+ | 2014-08-01 22:21:09 |  72 | | 2014-10-05 00:15:23 |  69 | | 2014-12-20 11:55:45 |  73 | | 2015-02-04 23:45:29 |  76 | | 2015-04-02 03:51:14 |  58 | | 2015-06-19 22:25:06 |  15 | +---------------------+-----+  mysql> select * m_feedbacks limit 5; +----+-----------+-------+---------------------+---------+ | id | x_id      | m     | m_timestamp         | user_id | +----+-----------+-------+---------------------+---------+ |  1 |         1 |     1 | 2015-02-04 23:45:29 |       1 | |  2 |         1 |     2 | 2015-04-24 23:00:37 |       1 | |  3 |         1 |     2 | 2014-09-25 02:51:37 |       1 | |  4 |         1 |     0 | 2014-09-29 03:28:01 |       1 | |  5 |         1 |     0 | 2014-12-11 16:15:09 |       1 | +----+-----------+-------+---------------------+---------+ 5 rows in set (0.00 sec) 

thanks in advance

your first approach close, select x y work. value not displayed inspect method. first value array , call cnt on see working.

when call group, key value of group method calculation.

i think best way want, can use pluck:

ab.where("m = 1").group("unix_timestamp(m_timestamp) div 6048000 ").pluck("m_timestamp, count(*)").to_h 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -