muratguzel / letsrate

The best way to add rating capabilities to your rails application and your activerecord models.
http://letsrate.herokuapp.com
198 stars 234 forks source link

Single Table Inheritance support #64

Closed migu0 closed 10 years ago

migu0 commented 10 years ago

I have two models:

class Resource
end

class Tutorial < Resource
  letsrate_rateable "name"
end

When I rate a Tutorial, RatingCache saves the correct type (Tutorial):

=> #<RatingCache id: 41, cacheable_id: 9, cacheable_type: "Tutorial", avg: 3.0, qty: 1, dimension: "name", created_at: "2014-03-12 05:06:09", updated_at: "2014-03-12 05:06:09">

However, when I call the average method in the Letsrate module, it searches for RatingCaches of the parent class (Resource), not the child class (Tutorial). In other words, when I call self.send("#{dimension}_average"), I get this SQL:

RatingCache Load (0.2ms)  SELECT  "rating_caches".* FROM "rating_caches"  WHERE "rating_caches"."cacheable_id" = $1 AND "rating_caches"."cacheable_type" = $2 AND "rating_caches"."dimension" = 'name'  ORDER BY "rating_caches"."id" ASC LIMIT 1  [["cacheable_id", 9], ["cacheable_type", "Resource"]] => nil

What needs to be changed so when saving and retrieving RatingCaches, the cacheable_type matches (either Tutorial or Resource)?

migu0 commented 10 years ago

Just realised that if I flush all tables and put the letsrate_rateable inside the child class/model it works fine.