From 12fa867295978d02f6de56e5fa36343170c6ce5c Mon Sep 17 00:00:00 2001 From: Mateusz Date: Tue, 24 Apr 2018 13:24:19 +0200 Subject: [PATCH] Fixes for active_record_uuid --- README.md | 2 ++ lib/recommendable/rater/recommender.rb | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d0504d6..0aaac79 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ Recommendable.configure do |config| end ``` +Important: in case of `active_record` with id of type `uuid`, use `:active_record_uuid`. + ## Usage In your model that will be receiving recommendations: diff --git a/lib/recommendable/rater/recommender.rb b/lib/recommendable/rater/recommender.rb index 4424427..0bef49d 100644 --- a/lib/recommendable/rater/recommender.rb +++ b/lib/recommendable/rater/recommender.rb @@ -8,8 +8,9 @@ module Recommender # @return [Array] An array of instances of your user class def similar_raters(limit = 10, offset = 0) ids = Recommendable.redis.zrevrange(Recommendable::Helpers::RedisKeyMapper.similarity_set_for(id), 0, -1) + ids = sanitize_ids(ids, self.class) - order = ids.map { |id| "#{Recommendable.config.user_class.quoted_table_name}.#{Recommendable.config.user_class.quoted_primary_key} = %d DESC" }.join(', ') + order = ids.map { |id| "#{Recommendable.config.user_class.quoted_table_name}.#{Recommendable.config.user_class.quoted_primary_key} = ? DESC" }.join(', ') order = self.class.send(:sanitize_sql_for_assignment, [order, *ids]) Recommendable.query(self.class, ids).order(order).limit(limit).offset(offset) @@ -26,14 +27,21 @@ def recommended_for(klass, limit = 10, offset = 0) ids = Recommendable.redis.zrevrange(recommended_set, 0, -1, :with_scores => true) ids = ids.select { |id, score| score > 0 }.map { |pair| pair.first } + ids = sanitize_ids(ids, klass) - order = ids.map { |id| "#{klass.quoted_table_name}.#{klass.quoted_primary_key} = %d DESC" }.join(', ') + order = ids.map { |id| "#{klass.quoted_table_name}.#{klass.quoted_primary_key} = ? DESC" }.join(', ') order = klass.send(:sanitize_sql_for_assignment, [order, *ids]) Recommendable.query(klass, ids).order(order).limit(limit).offset(offset) end private + # Sanitizes ids using klass type mapping + # @private + def sanitize_ids(ids, klass) + ids.map{ |id| klass.new(klass.primary_key => id).send(klass.primary_key) }.compact + end + # Removes an item from a user's set of recommendations # @private def unrecommend(obj)