From f3ced6d96cbeaed4f7f91cdc1df13e9dd9a4e10f Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 16 Dec 2010 16:37:13 -0800 Subject: [PATCH] randomize collections of similar contacts on people#show --- app/controllers/application_controller.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7352e0219..bae611e4d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -49,13 +49,24 @@ class ApplicationController < ActionController::Base end end - def similar_people contact + def similar_people contact, opts={} + opts[:limit] ||= 5 aspect_ids = contact.aspect_ids + count = Contact.count(:user_id => current_user.id, + :person_id.ne => contact.person.id, + :aspect_ids.in => aspect_ids) + + if count > opts[:limit] + offset = rand(count-opts[:limit]) + else + offset = 0 + end + contacts = Contact.all(:user_id => current_user.id, :person_id.ne => contact.person.id, :aspect_ids.in => aspect_ids, - :limit => 5, - :order => 'updated_at desc') + :skip => offset, + :limit => opts[:limit]) contacts.collect!{ |contact| contact.person } end end