sql - Rails ActiveRecord sum of users voted? -
the result want number of users voted rateable_id = '3'?
example: can see in table rates, rater_id: 1 , rater_id: 8 have voted rateable_id = 3. makes 2 users.
my question how display in view?
this in ranking_controller.rb:
class rankingcontroller < applicationcontroller def index @rankings = rate.find(:all) end end
this 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
you use below:
@raters_count = rate.select(:rater_id).where(rateable_id: 3, rateable_type: 'bboy' ).distinct.count
Comments
Post a Comment