@@ -8,8 +8,9 @@ module Recommender
8
8
# @return [Array] An array of instances of your user class
9
9
def similar_raters ( limit = 10 , offset = 0 )
10
10
ids = Recommendable . redis . zrevrange ( Recommendable ::Helpers ::RedisKeyMapper . similarity_set_for ( id ) , 0 , -1 )
11
+ ids = sanitize_ids ( ids , self . class )
11
12
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 ( ', ' )
13
14
order = self . class . send ( :sanitize_sql_for_assignment , [ order , *ids ] )
14
15
15
16
Recommendable . query ( self . class , ids ) . order ( order ) . limit ( limit ) . offset ( offset )
@@ -26,14 +27,21 @@ def recommended_for(klass, limit = 10, offset = 0)
26
27
27
28
ids = Recommendable . redis . zrevrange ( recommended_set , 0 , -1 , :with_scores => true )
28
29
ids = ids . select { |id , score | score > 0 } . map { |pair | pair . first }
30
+ ids = sanitize_ids ( ids , klass )
29
31
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 ( ', ' )
31
33
order = klass . send ( :sanitize_sql_for_assignment , [ order , *ids ] )
32
34
Recommendable . query ( klass , ids ) . order ( order ) . limit ( limit ) . offset ( offset )
33
35
end
34
36
35
37
private
36
38
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
+
37
45
# Removes an item from a user's set of recommendations
38
46
# @private
39
47
def unrecommend ( obj )
0 commit comments