Rails beginner
Rails beginner

Reputation: 14514

Rails help how to create rating controller

I am trying to create a simple rating controller based on this answer. Rails 3 rateable model - How to create ajax rating?

In my table I have:

rating_score => The current score
ratings => The number of ratings which led to the score

Here is my rate action:

  def rate
    @konkurrancer = Konkurrancer.find(params[:id])
    @container = "Konkurrancer"[email protected]_s

    @konkurrancer.rating_score += params[:rating].to_i
    @konkurrancer.ratings += 1
    @konkurrancer.save

    respond_to do |format|
      format.js
    end
  end

My log:

Started POST "/konkurrancers/rate/38" for 127.0.0.1 at 2011-04-03 19:28:13 +0200

  Processing by KonkurrancersController#rate as JS
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"q+CkUReuh0mmkSjRcd+U/JmB1tV
FWHRpOeIFxy20afs=", "vind"=>{"rating"=>"6"}, "commit"=>"Gem Vind", "id"=>"38"}
  ←[1m←[35mKonkurrancer Load (0.0ms)←[0m  SELECT `konkurrancers`.* FROM `konkurr
ancers`
  ←[1m←[36mCACHE (0.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `konkurrancers`←
[0m
  ←[1m←[35mCACHE (0.0ms)←[0m  SELECT `konkurrancers`.* FROM `konkurrancers`
  ←[1m←[36mKonkurrancer Load (0.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `kon
kurrancers` WHERE (`konkurrancers`.`cached_slug` = '38') LIMIT 1←[0m
  ←[1m←[35mSQL (15.6ms)←[0m  SELECT sluggable_id FROM slugs WHERE ((slugs.slugga
ble_type = 'Konkurrancer' AND slugs.name = '38' AND slugs.sequence = 1))
  ←[1m←[36mKonkurrancer Load (0.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `kon
kurrancers` WHERE (`konkurrancers`.`id` = 38) LIMIT 1←[0m
  ←[1m←[35mSQL (0.0ms)←[0m  BEGIN
  ←[1m←[36mSlug Load (0.0ms)←[0m  ←[1mSELECT `slugs`.* FROM `slugs` WHERE (`slug
s`.sluggable_id = 38 AND `slugs`.sluggable_type = 'Konkurrancer') ORDER BY id DE
SC LIMIT 1←[0m
  ←[1m←[35mAREL (0.0ms)←[0m  UPDATE `konkurrancers` SET `ratings` = 84, `updated
_at` = '2011-04-03 17:28:13' WHERE (`konkurrancers`.`id` = 38)
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mCOMMIT←[0m
Rendered konkurrancers/_rating.html.erb (0.0ms)
Rendered konkurrancers/rate.js.erb (31.2ms)
Completed 200 OK in 250ms (Views: 171.6ms | ActiveRecord: 15.6ms)

The problem is that the param rating not gets saved.

Upvotes: 0

Views: 286

Answers (1)

Marc Talbot
Marc Talbot

Reputation: 2059

If you look at the last SQL line displayed, you'll see that it's selecting all of the ratings from your ratings table. That makes me think that @konkurrancer.ratings is actually an array (a collection of ratings), and you're trying to add 1 to it when you do @koncurrencer.ratings += 1. That says this is probably a class design issue. Did you mean to have a Koncurrencer object that has many ratings objects, or did you mean for the ratings to simply be an integer sum (as it appears to be in the post that you referenced)?

Upvotes: 1

Related Questions