Search code examples
ruby-on-railsarraysrubywhere-clausepluck

Filtering array with reject + conditions in rails


I'm trying to filter an array of matched records with multiple conditions and can't seem to figure it out, I've tried a lot of different things and googled everything but nothing... here's the code:

if @post.last_post?
  @recommendations = @course.recommendations_for_subscriber(subscriber, category, language).first(3)
end

and the helper method

def recommendations_for_subscriber subscriber, category, language
  course_ids = subscriber.courses.pluck(:id)
  recommendations.reject { |c| course_ids.include? c.id }
end

what I'm trying to do is to pass in category and language as a conditions to only having results with the same conditions set. ".where" won't work because it doesn't work well with an array and I can't seem to pass it in as an addition to the reject. Any ideas would be super appreciated and rewarded with cake if we ever run into each other!


Solution

  • I guess this will resolve your problem:

    def recommendations_for_subscriber subscriber, category, language
      Recommendation.where('courses.id NOT IN(?)', subscriber.courses.pluck(:id))
    end