Skip to content

Commit 8cf2d9b

Browse files
authored
Merge pull request #123 from mkonikowski/master
Fixes for active_record_uuid
2 parents 70aed1f + 12fa867 commit 8cf2d9b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Recommendable.configure do |config|
7777
end
7878
```
7979

80+
Important: in case of `active_record` with id of type `uuid`, use `:active_record_uuid`.
81+
8082
## Usage
8183

8284
In your model that will be receiving recommendations:

lib/recommendable/rater/recommender.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ module Recommender
88
# @return [Array] An array of instances of your user class
99
def similar_raters(limit = 10, offset = 0)
1010
ids = Recommendable.redis.zrevrange(Recommendable::Helpers::RedisKeyMapper.similarity_set_for(id), 0, -1)
11+
ids = sanitize_ids(ids, self.class)
1112

12-
order = ids.map { |id| "#{Recommendable.config.user_class.quoted_table_name}.#{Recommendable.config.user_class.quoted_primary_key} = %d DESC" }.join(', ')
13+
order = ids.map { |id| "#{Recommendable.config.user_class.quoted_table_name}.#{Recommendable.config.user_class.quoted_primary_key} = ? DESC" }.join(', ')
1314
order = self.class.send(:sanitize_sql_for_assignment, [order, *ids])
1415

1516
Recommendable.query(self.class, ids).order(order).limit(limit).offset(offset)
@@ -26,14 +27,21 @@ def recommended_for(klass, limit = 10, offset = 0)
2627

2728
ids = Recommendable.redis.zrevrange(recommended_set, 0, -1, :with_scores => true)
2829
ids = ids.select { |id, score| score > 0 }.map { |pair| pair.first }
30+
ids = sanitize_ids(ids, klass)
2931

30-
order = ids.map { |id| "#{klass.quoted_table_name}.#{klass.quoted_primary_key} = %d DESC" }.join(', ')
32+
order = ids.map { |id| "#{klass.quoted_table_name}.#{klass.quoted_primary_key} = ? DESC" }.join(', ')
3133
order = klass.send(:sanitize_sql_for_assignment, [order, *ids])
3234
Recommendable.query(klass, ids).order(order).limit(limit).offset(offset)
3335
end
3436

3537
private
3638

39+
# Sanitizes ids using klass type mapping
40+
# @private
41+
def sanitize_ids(ids, klass)
42+
ids.map{ |id| klass.new(klass.primary_key => id).send(klass.primary_key) }.compact
43+
end
44+
3745
# Removes an item from a user's set of recommendations
3846
# @private
3947
def unrecommend(obj)

0 commit comments

Comments
 (0)