gpt4 book ai didi

mysql - ActiveRecord属性总和用户,投票查询rails?

转载 作者:行者123 更新时间:2023-11-29 08:08:41 24 4
gpt4 key购买 nike

我想在我的 View 中显示与“rateable_id: 3”相关的所有属性。我是查询数据库并在 View 中显示结果的新手。另外,我想在 View 中显示“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

最佳答案

您可能希望在 show 中执行此操作 Controller 中的方法,因为它将调用特定的 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

关于mysql - ActiveRecord属性总和用户,投票查询rails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22177731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com