Skip to content

Commit

Permalink
Return ActiveRecord::Relations for recommended_* methods
Browse files Browse the repository at this point in the history
A supposedly database-compatible way to do this; possibly addresses #96
and #103, but I'd like user feedback first.

Signed-off-by: David Celis <[email protected]>
  • Loading branch information
davidcelis committed Mar 11, 2015
1 parent e7c0870 commit d78e6f7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/recommendable/rater/recommender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module Recommender
# @return [Array] An array of instances of your user class
def similar_raters(count = 10, offset = 0)
ids = Recommendable.redis.zrevrange(Recommendable::Helpers::RedisKeyMapper.similarity_set_for(id), offset, count - 1)
Recommendable.query(self.class, ids).sort_by { |user| ids.index(user.id.to_s) }

order = ids.map { |id| "`#{Recommendable.config.user_class.table_name}`.`id` = %d DESC" }.join(', ')
order = self.class.send(:sanitize_sql_for_assignment, [order, *ids])

Recommendable.query(self.class, ids).order(order)
end

private
Expand All @@ -25,7 +29,9 @@ def recommended_for(klass, count = 10, offset = 0)
ids = Recommendable.redis.zrevrange(recommended_set, offset, count - 1, :with_scores => true)
ids = ids.select { |id, score| score > 0 }.map { |pair| pair.first }

Recommendable.query(klass, ids).sort_by { |record| ids.index(record.id.to_s) }
order = ids.map { |id| "`#{klass.table_name}`.`id` = %d DESC" }.join(', ')
order = klass.send(:sanitize_sql_for_assignment, [order, *ids])
Recommendable.query(klass, ids).order(order)
end

# Removes an item from a user's set of recommendations
Expand Down

0 comments on commit d78e6f7

Please sign in to comment.