Simple Rails ActiveRecord stars count query? -
this simple question. how retrieve 'rateable_id's sum of stars count 'dimension': foundation in descending order? chain of queries use?
in table rates:
- !ruby/object:rate attributes: id: 11 rater_id: 1 rateable_id: 3 rateable_type: bboy stars: 5.0 dimension: foundation created_at: 2014-02-25 09:33:23.000000000 z updated_at: 2014-02-25 09:33:23.000000000 z - !ruby/object:rate attributes: id: 12 rater_id: 1 rateable_id: 3 rateable_type: bboy stars: 5.0 dimension: originality created_at: 2014-02-25 09:33:24.000000000 z updated_at: 2014-02-25 09:33:24.000000000 z - !ruby/object:rate attributes: id: 13 rater_id: 1 rateable_id: 3 rateable_type: bboy stars: 5.0 dimension: dynamics created_at: 2014-02-25 09:33:25.000000000 z updated_at: 2014-02-25 09:33:25.000000000 z - !ruby/object:rate attributes: id: 14 rater_id: 1 rateable_id: 3 rateable_type: bboy stars: 5.0 dimension: execution created_at: 2014-02-25 09:33:26.000000000 z updated_at: 2014-02-25 09:33:26.000000000 z - !ruby/object:rate attributes: id: 15 rater_id: 1 rateable_id: 3 rateable_type: bboy stars: 5.0 dimension: battle created_at: 2014-02-25 09:33:27.000000000 z updated_at: 2014-02-25 09:33:27.000000000 z - !ruby/object:rate attributes: id: 16 rater_id: 1 rateable_id: 5 rateable_type: bboy stars: 5.0 dimension: foundation created_at: 2014-02-25 09:36:30.000000000 z updated_at: 2014-02-25 09:36:30.000000000 z - !ruby/object:rate attributes: id: 17 rater_id: 1 rateable_id: 5 rateable_type: bboy stars: 5.0 dimension: originality created_at: 2014-02-25 09:36:31.000000000 z updated_at: 2014-02-25 09:36:31.000000000 z - !ruby/object:rate attributes: id: 18 rater_id: 1 rateable_id: 5 rateable_type: bboy stars: 5.0 dimension: dynamics created_at: 2014-02-25 09:36:31.000000000 z updated_at: 2014-02-25 09:36:31.000000000 z - !ruby/object:rate attributes: id: 19 rater_id: 1 rateable_id: 5 rateable_type: bboy stars: 5.0 dimension: battle created_at: 2014-02-25 09:36:32.000000000 z updated_at: 2014-02-25 09:36:32.000000000 z - !ruby/object:rate attributes: id: 25 rater_id: 8 rateable_id: 3 rateable_type: bboy stars: 1.0 dimension: foundation created_at: 2014-03-04 14:06:46.000000000 z updated_at: 2014-03-04 14:06:46.000000000 z - !ruby/object:rate attributes: id: 26 rater_id: 8 rateable_id: 3 rateable_type: bboy stars: 1.0 dimension: originality created_at: 2014-03-04 14:06:49.000000000 z updated_at: 2014-03-04 14:06:49.000000000 z - !ruby/object:rate attributes: id: 27 rater_id: 8 rateable_id: 3 rateable_type: bboy stars: 1.0 dimension: dynamics created_at: 2014-03-04 14:06:51.000000000 z updated_at: 2014-03-04 14:06:51.000000000 z - !ruby/object:rate attributes: id: 28 rater_id: 8 rateable_id: 3 rateable_type: bboy stars: 1.0 dimension: execution created_at: 2014-03-04 14:06:53.000000000 z updated_at: 2014-03-04 14:06:53.000000000 z - !ruby/object:rate attributes: id: 29 rater_id: 8 rateable_id: 3 rateable_type: bboy stars: 1.0 dimension: battle created_at: 2014-03-04 14:06:54.000000000 z updated_at: 2014-03-04 14:06:54.000000000 z
thank you!i appreciate help!
well, if n-th time i've seen this. way ask it, try this:
rate.where(dimension: "foundation").sum(:stars, group: :rateable_id, order: "sum_stars desc").map{|a,b| {"rateable_id"=>a, "sum_stars"=>b}}
Comments
Post a Comment