mysql - Can't select rows grouping by created_at -


i have table:

+----+----------------------+---------------------+ | id | implemented_features | created_at          | +----+----------------------+---------------------+ |  1 |                   19 | 2013-07-18 04:10:12 | |  2 |                    6 | 2013-07-18 04:10:12 | |  3 |                   26 | 2013-07-19 04:10:12 | |  4 |                   11 | 2013-07-19 04:10:12 | |  5 |                    1 | 2013-07-20 04:10:12 | +----+----------------------+---------------------+ 

when query directly via mysql works want

select date(created_at) date, sum(implemented_features) sum summaries group date(created_at); 

but when try convert query activerecord syntax returns me nil.

2.0.0p0 :035 > summary.select("date(created_at) date, sum(implemented_features)").group("date(created_at)")   summary load (0.5ms)  select date(created_at) date, sum(implemented_features) `summaries` group date(created_at)  => #<activerecord::relation [#<summary id: nil>]>  

as see, final queries equal in both examples. why don't work in case of activerecord?

using rails 4.0.0, ruby 2.0, mysql db in rails project.

i think you're little confused console output.

you're saying this:

summary.select("date(created_at) date, sum(implemented_features)")... 

so returned summary instances (wrapped in activerecord::relation) don't have of usual summary attributes: no id, no created_at, no implemented_featured, etc. when call inspect on activerecord object, wants show what's inside object , means wants show contained database attributes; summary instances don't have of usual attributes see things <summary id: nil>.

fear not, values selected there. if say:

summary.select(...).map(&:date) 

you should see date(created_at) date values. if add alias sum(implemented_features) can extract sums using alias method name.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -