Question

I would like to show in my view all the attributes related to "rateable_id: 3" for example. I am new to querying dbs and showing results in view. Also I want to display in view the sum of "rater_id:"( how many users have voted) for "rateable_id: 3" for example.

This is in my ranking_controller.rb:

class RankingController < ApplicationController
  def index
    @rankings = Rate.find(:all)
  end
end

This is in my 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
Was it helpful?

Solution

You would likely want to do this in the show method in your controller, as that will call a specific Ranking:

class RankingController < ApplicationController
  def index
    @rankings = Rate.find(:all)
  end

  def show
    @ranking = Rate.where(:rateable_id => params[:id])
  end
end

Then, in your show view, you could use count to get the total number of Rate records returned against unique rater_id values:

@ranking.map(&:rater_id).uniq.count

OTHER TIPS

For the sums:

You can use two queries

@rankings_by_raters = Rate.group(:rater_id).count
@rankings_by_rateable = Rate.group(:rateable_id).count

which will give you two hashes with the rater and rateable' s ids as keys and the relevant counts as values.

For the show:

@rankings_for_rate = Rate.where(rater_id: params[:rater_id])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top