So far, I have this query for the gem PublicActivity. One (that I'm aware of) problem with it, is if the activity.trackable is nil, it will still include it in the query, thus forcing me in the view to run the conditional, if activity.trackable
, which basically helps if the trackable object has been deleted, so you don't try to access a nil object.
PublicActivity::Activity.
includes(:trackable, :owner).
order('created_at DESC').
where(recipient_id: current_user, kind: "Notification", read: false).
where('owner_id not in (?)', current_user).
size
I want to include a where
query to only query the activities where the activity.trackable IS NOT NULL
.
In the view, with: activity.trackable.nil?
, I can see if the trackable object is nil, but I can't figure out how to do this in the query, even after searching S.O. and rails api/docs. Any guidance would be appreciated.
EDIT
Even after you destroy the trackable
object, the trackable_id
will still be stored on the activity
. Below is an example of an activity after I destroy the trackable
object.
#<PublicActivity::Activity id: 389, trackable_id: 865, trackable_type: "Vote", owner_id: nil, owner_type: nil, key: "vote.update", parameters: {}, recipient_id: nil, recipient_type: nil, created_at: "2014-06-22 14:33:37", updated_at: "2014-06-22 14:33:37", kind: nil, read: false>