0

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!

LessPixels
  • 145
  • 7
  • What's the error message you're getting? What is this currently outputting? Also, why are you passing `category` and `language` to the `recommendations_for_subscriber` method if these values are not being used? – DaniG2k Sep 27 '15 at 16:41

1 Answers1

0

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
usmanali
  • 2,028
  • 2
  • 27
  • 38
  • Thank you so much! That worked perfectly. Although I feel a little retarded now. Let me know if you are ever in Stockholm and I will repay you with cake! – LessPixels Sep 27 '15 at 17:07
  • Glad that it worked. I would certainly have the cake from you if I ever visit Stockholm :) – usmanali Sep 27 '15 at 17:13
  • 1
    You can eliminate the fragment with `Recommendation.where.not(courses: { id: subscriber.courses.pluck(:id) })` as well. – ahmacleod Sep 27 '15 at 17:35