Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Empty dimension problem #68

Open
katafrakt opened this issue Mar 20, 2012 · 0 comments
Open

Empty dimension problem #68

katafrakt opened this issue Mar 20, 2012 · 0 comments

Comments

@katafrakt
Copy link

When you set up axr without dimensions in Rails 3.1 and vote, dimension=&... is always passed in url and because of that @model.rate(stars, current_user, "") is called instead of @model.rate(stars, current_user, nil). This affects somehow a cached rating which does not get updated after the first call. This is what happens in my rails console:

ruby-1.9.3-p125 :019 > p.rate(1, User.find(1))
  User Load (1.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  Rate Load (1.7ms)  SELECT "rates".* FROM "rates" WHERE "rates"."rateable_id" = 7 AND "rates"."rateable_type" = 'Part' AND "rates"."dimension" IS NULL AND "rates"."rater_id" = 1 LIMIT 1
  Rate Load (1.8ms)  SELECT "rates".* FROM "rates" WHERE "rates"."rateable_id" = 7 AND "rates"."rateable_type" = 'Part' AND "rates"."dimension" IS NULL AND "rates"."rater_id" = 1 LIMIT 1
   (0.8ms)  BEGIN
   (57.0ms)  UPDATE "rates" SET "stars" = 1, "updated_at" = '2012-03-17 05:54:35.885397' WHERE "rates"."id" = 6
   (9.6ms)  COMMIT
   (1.9ms)  SELECT SUM("rates"."stars") AS sum_id FROM "rates" WHERE "rates"."rateable_id" = 7 AND "rates"."rateable_type" = 'Part' AND "rates"."dimension" IS NULL
   (1.5ms)  SELECT COUNT(*) FROM "rates" WHERE "rates"."rateable_id" = 7 AND "rates"."rateable_type" = 'Part' AND "rates"."dimension" IS NULL
   (0.8ms)  BEGIN
   (1.8ms)  UPDATE "parts" SET "average_rating" = 2.5, "updated_at" = '2012-03-17 05:54:35.969188' WHERE "parts"."id" = 7
   (4.2ms)  COMMIT
 => true 
ruby-1.9.3-p125 :020 > p.rate(2, User.find(1), "")
  User Load (1.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  Rate Load (1.7ms)  SELECT "rates".* FROM "rates" WHERE "rates"."rateable_id" = 7 AND "rates"."rateable_type" = 'Part' AND "rates"."dimension" IS NULL AND "rates"."rater_id" = 1 LIMIT 1
  Rate Load (1.4ms)  SELECT "rates".* FROM "rates" WHERE "rates"."rateable_id" = 7 AND "rates"."rateable_type" = 'Part' AND "rates"."dimension" IS NULL AND "rates"."rater_id" = 1 LIMIT 1
   (0.9ms)  BEGIN
   (1.4ms)  UPDATE "rates" SET "stars" = 2, "updated_at" = '2012-03-17 05:57:32.882623' WHERE "rates"."id" = 6
   (54.7ms)  COMMIT
 => nil 

The issue can be easily fixed by making your controller method looking like:

  def rate
    @part = Part.find(params[:id])
    params[:dimension].blank? ? dimension = nil : dimension = params[:dimension]
    @part.rate(params[:stars], current_user, dimension)
    respond_to do |format|
      format.js
    end
  end

or even simplier

@part.rate(params[:stars], current_user)

but maybe it would be better to solve it in axr itself.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant