How to always return a set number of records when using find_related_tags with acts-as-taggable-on

Posted by hadees on Stack Overflow See other posts from Stack Overflow or by hadees
Published on 2010-06-05T23:43:17Z Indexed on 2010/06/05 23:52 UTC
Read the original article Hit count: 237

I'm using the acts-as-taggable-on gem and I need to use find_related_tags on my survey model to get back 3 surveys every time. In the event there aren't always 3 related I need to pick how ever many are related plus some random ones to get to 3. Additionally I have a method I wrote called completed_survey_ids which return an array of survey_ids that shouldn't be used because the user has already completed them. Also there is a rare case that there won't be enough surveys because the user has completed them all so in that event it is okay to return less surveys then requested.

I did write a named_scope to handle getting rid of the completed_survey_ids that I think works

  named_scope :not, lambda { |survey_ids| {:conditions => "id NOT IN (#{survey_ids.join(',')})" } }

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord