Ask A Question

Notifications

You’re not receiving notifications from this thread.

Activity Feed From Scratch Discussion

Could you post the source code to this?

Reply

Absolutely! Just posted it here: https://github.com/excid3/g...

Reply
Kohl Kohlbrenner Kohl Kohlbrenner

event.eventable is an instance of a class right

Reply

Yep! That's correct. It's actually another model instance but just given the name of "eventable" because it could be of various types of models.

Reply

Chris,
How do we handle deletion dependencies here? E.g. say I setup an event to be trigged on a `comment` creation, or when I upload an image. But what happens when I delete that comment or I delete that image. Ideally, I would like to be able to delete the event that is related to that specific recently deleted comment or image.

What's the best way to do this?

Reply

This is a fantastic question. If you setup the reverse dependency and set the dependent destroy option, that will delete the events when you delete the comment.

class Comment
has_many :events, as: :eventable, dependent: :destroy
end

Reply

Love it keep it up Chris

Reply

Hey Chris. Thanks for all the awesome tutorials. If I wanted to add a link to "Unfollow" and delete the event, what would that look like?

Reply
It looks like the `lookup_context.template_exists?()` method now requires the prefixes to be an array, or an enumerable that responds to `.map()`, instead of just a string. This was changed as previous versions of Rails (< 3.1) accepted a string prefix. I'm not sure why it works in the episode, since you're using Rails 4; maybe it was a soft deprecation?

https://apidock.com/rails/ActionView/LookupContext/ViewPaths/template_exists%3f
Reply

Great video, man. I had originally figure out how to do this on my own, and was like, "that's me!" when you mentioned how not to do it. Thanks for showing an alternative to "if activity type is this, show this". I was able to refactor quite a bit of code.

Reply

I have my events set up. Some of the models I have are listings and reviews. Reviews belong to listings. If someone leaves a review for a listing, the event saves both the review id (as eventable_id) and the listing id (as parent_id). So I can link to both in the view.

My problem is that if I deleted the listing, the review event remains, and rails throws an error (can't find listing with id 25, for example).

Here is my listing model:

class Listing < ApplicationRecord
  belongs_to :user
  has_many :reviews, dependent: :destroy
  has_many :activities, as: :eventable, dependent: :destroy

My reviews model also has
has_many :activities, as: :eventable, dependent: :destroy

The issue I'm having is that the listing gets deleted, and the reviews and events associated with that listing get deleted. But the events associated with the reviews do not get deleted. Did I do something wrong in the association?

Reply

Is this something where I would need to use a cascade? I do not understand the differences.
*Update, it seems like the immediate events are not being deleted along with the listing as well.

Reply

So I managed to fix this by changing it from :destroy to :delete_all.
has_many :activities, as: :eventable, dependent: :delete_all

Reply

I see Chris is wearing a digg T-shirt. That takes me back. I used to work in a co-working space in San Francisco. Digg had to scale down and the space bought their phone booths for cell phones, so that you could make somewhat private calls.

Reply

Hahaha! That is awesome. I loved Digg. I used to watch Diggnation sooo much.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.