0

I'm trying to hide activities where their owners have deleted their accounts. So I added this

PublicActivity::Activity.order("created_at DESC").where( recipient: current_user).where.not(owner: nil)

but it returns

undefined method `image' for nil:NilClass

meaning it's adding activities with deleted users to the record. How can I hide them?

Malek Zalfana
  • 318
  • 2
  • 11
  • you are not making any calls to 'image' on that line, can you run that line on your rails console and still gives you the same error? – MZaragoza Aug 13 '16 at 14:38
  • I am calling images in the view, the thing is since this is happening then the record is including activities with owner: nil. In the console (with same users) it returns # – Malek Zalfana Aug 13 '16 at 14:43
  • can you share the line of the view that is giving you problems – MZaragoza Aug 13 '16 at 14:47
  • I tried like 5 different ways, the error is coming from (activity.owner.image). since the user no more exists – Malek Zalfana Aug 13 '16 at 14:55
  • you could try something like this `<%= activity.owner.image.id if activity.owner.image %>` – MZaragoza Aug 13 '16 at 14:58
  • No, I need to not include the activity from the first place in the record, thanks for the help tho – Malek Zalfana Aug 13 '16 at 16:52
  • Your code seems correct. Still, you may try: `PublicActivity::Activity.order("created_at DESC").where("recipient = ? && owner IS NOT NULL", current_user)` – Abhi Aug 13 '16 at 18:41
  • @Abhi retuned PublicActivity::Activity.order("created_at DESC").where("recipient = ? && owner IS NOT NULL", current_user) – Malek Zalfana Aug 13 '16 at 19:17
  • @MalekZalfana I didn't get you. If you tried the above, what's the outcome? – Abhi Aug 14 '16 at 08:16
  • @abhi I replied with the outome of your recommendation and the error in question – Malek Zalfana Aug 14 '16 at 08:21

0 Answers0