Rails ActiveRecord第二属性的星数之和?

时间:2014-03-27 18:12:27

标签: ruby-on-rails activerecord

我想在视图中显示哪个rateable_id拥有最多的stars.count为“dimension:foundation”?

这是我的表费率:

- !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

这是我的tabke bboys:

- !ruby/object:Bboy
  attributes:
    id: 3
    name: taisuke
    crew: japan
    country: JAP
    created_at: 2014-02-25 09:32:56.000000000 Z
    updated_at: 2014-02-25 09:32:56.000000000 Z
    avatar_file_name: justdoit.jpeg
    avatar_content_type: image/jpeg
    avatar_file_size: 9297
    avatar_updated_at: 2014-02-25 09:32:55.000000000 Z
- !ruby/object:Bboy
  attributes:
    id: 4
    name: roxrite
    crew: us
    country: us
    created_at: 2014-02-25 09:35:40.000000000 Z
    updated_at: 2014-02-25 09:35:40.000000000 Z
    avatar_file_name: 
    avatar_content_type: 
    avatar_file_size: 
    avatar_updated_at: 
- !ruby/object:Bboy
  attributes:
    id: 5
    name: me
    crew: japan
    country: JAP
    created_at: 2014-02-25 09:36:23.000000000 Z
    updated_at: 2014-02-25 09:36:23.000000000 Z
    avatar_file_name: justdoit.png
    avatar_content_type: image/png

感谢您的时间和专业知识!你正在帮助我1000!

1 个答案:

答案 0 :(得分:1)

这个两步解决方案可能有效:

most_starred_id = Rate.where(dimension: 'foundation').select("rateable_id, sum('stars') as total_stars").group(:rateable_id).order(:total_stars => :desc).first.rateable_id

most_starred = Bboy.find(most_starred).name