jeudi 10 décembre 2015

Best way to filter unseen videos in Rails

We build a video-section for our users. The user can filter the videos by rating/views/date. Also the user can decide to hide already seen videos. This is where i struggle a little bit.

right now i have a solution, which is working, but doesnt seem to perform great.

if @filter == "newest"
  if @unseen
    ids = Videothek::Video.where(videothek_category_id: categories).pluck(:id)
    views = Videothek::Video::View.where(user_id: current_user.id).pluck(:video_id)
    unseen = ids - views #ids der ungesehenen videos
    @videos = Videothek::Video.where(id: unseen).order("created_at DESC")
  else
    @videos = Videothek::Video.where(videothek_category_id: categories).order("created_at DESC")
  end
end

i thought it must be possible to do with a scope, like Videothek::Video.unseen(current_user).order(.....)

A Video has_many Views, but i struggle to get the join running, as i just want the videos, that DONT have an association with an videothek_video_view, where user_id = 1 (or current_user.id).

can somebody help me out?

btw: we are on RoR3

Aucun commentaire:

Enregistrer un commentaire