0

我正在使用Rails 4並具有notifsusers模型。Rails使用自定義模型方法

每個notifhas_manyusers(又名哪個user已見過notif)。我試圖獲得所有沒有被current_user看到的通知。爲了檢查current_user已經看到了通知符我使用這個模型類:

def seen_by?(u) 
    return self.users.include?(u) 
end 

如何使用一個電話來獲取所有notifs其中seen_by(current_user)true

喜歡的東西:

Notif.where(seen_by(current_user): true) 

回答

0

基本上你需要一個在這裏反連接模式,可以與left join/is nullnot in/subquerynot exists/subquery檢查來進行,例如。第一個問題是,您不能在沒有原始sql(或重寫關聯)的情況下在on子句中指定條件。所以你可以去這裏用not in和一個單獨的查詢方法:

Notif.where.not(id: User.where(id: current_user_id).pluck[:notif_id])