我想要的结果只是投票给rateable_id ='3'的用户数量?
示例:正如您在我的表费率中所看到的,rater_id:1和rater_id:8已经投票选出rateable_id = 3.这使得有2个用户。
我的问题是如何在视图中显示?
这是在我的ranking_controller.rb中:
class RankingController < ApplicationController
def index
@rankings = Rate.find(:all)
end
end
这是我的表费率:
- !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
答案 0 :(得分:2)
您可以按如下方式使用它:
@raters_count = Rate.select(:rater_id).where(rateable_id: 3, rateable_type: 'Bboy' ).distinct.count
答案 1 :(得分:1)
有很多方法可以实现这一目标。
一个是
@ranking_count = Rate.where(rateable_id: 3).pluck(:rater_id).uniq.count
答案 2 :(得分:1)
或者这个
@ratings = Rate.group("dimension, rateable_id").select("rateable_id, dimension, count(rater_id) as rate_count")
#=> SELECT rateable_id, dimension,count(rater_id) as rate_count FROM rates GROUP BY dimension, rateable_id
然后
@ratings.each do |rating|
rating.dimension
rating.rateable_id
rating.rate_count
end
显然可以根据您的需要进行修改,但这样的事情应该足够了。由于您的问题与您的代码不同,因此比原始问题更灵活。您的问题只想处理1 rateable_id
,但您的代码会加载所有Rate
,因此我根据使用基于维度的Rate
方法检索所有rate_count
个对象来回答。
更广泛的方法可能是
@ratings = Rate.group("dimension, rateable_id, rateable_type").select("rateable_id, dimension,rateable_type, count(rater_id) as rate_count, avg(stars) as stars")
#=> SELECT rateable_id, dimension,rateable_type,count(rater_id) as rate_count, avg(stars) as stars FROM rates GROUP BY dimension, rateable_id,rateable_type
然后
@ratings.each do |rating|
rating.dimension
rating.rateable_id
rating.rateable_type
rating.rate_count
rating.stars
end
根据dimension
,rateable_id
和rateable_type
这应该根据上面的输出(psuedo)返回一些内容
[<Rate dimension:"foundation",rateable_id:3,rateable_type:"Bboy",rate_count:2, stars:3>,
<Rate dimension:"originality",rateable_id:3,rateable_type:"Bboy",rate_count:2, stars:3>,
<Rate dimension:"dynamics",rateable_id:3,rateable_type:"Bboy",rate_count:2, stars:3>,
<Rate dimension:"execution",rateable_id:3,rateable_type:"Bboy",rate_count:2, stars:3>,
<Rate dimension:"battle",rateable_id:3,rateable_type:"Bboy",rate_count:2, stars:3>,
<Rate dimension:"foundation",rateable_id:5,rateable_type:"Bboy",rate_count:1, stars:5>,
<Rate dimension:"originality",rateable_id:5,rateable_type:"Bboy",rate_count:1, stars:5>,
<Rate dimension:"dynamics",rateable_id:5,rateable_type:"Bboy",rate_count:1, stars:5>,
<Rate dimension:"battle",rateable_id:5,rateable_type:"Bboy",rate_count:1, stars:5>]