Skip to content

Commit

Permalink
Merge pull request #123 from mkonikowski/master
Browse files Browse the repository at this point in the history
Fixes for active_record_uuid
  • Loading branch information
davidcelis authored Apr 24, 2018
2 parents 70aed1f + 12fa867 commit 8cf2d9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 10 additions & 2 deletions lib/recommendable/rater/recommender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8cf2d9b

Please sign in to comment.