我想在我的视图中显示与“rateable_id:3”相关的所有属性。 我是新来的查询dbs并在视图中显示结果。另外,我想在视图中显示“rater_id:”(有多少用户投票)的总和,例如“rateable_id:3”。
这是在我的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 :(得分:1)
您可能希望在控制器的show
方法中执行此操作,因为这会调用特定的Ranking
:
class RankingController < ApplicationController
def index
@rankings = Rate.find(:all)
end
def show
@ranking = Rate.where(:rateable_id => params[:id])
end
end
然后,在show
视图中,您可以使用count
获取针对唯一Rate
值返回的rater_id
条记录总数:
@ranking.map(&:rater_id).uniq.count
答案 1 :(得分:1)
您可以使用两个查询
@rankings_by_raters = Rate.group(:rater_id).count
@rankings_by_rateable = Rate.group(:rateable_id).count
这将为您提供两个哈希值,其中rater
和rateable
的ID为关键字,相关值为值。
@rankings_for_rate = Rate.where(rater_id: params[:rater_id])