These are the two classes in question I have currently in my app.
class Post < ApplicationRecord has_rich_text :content end class VideoEmbed < ApplicationRecord include ActionText::Attachable end
For context, when users are writing a
Post, they can copy a valid video link (e.g. YouTube, Twitch) and it will show a thumbnail in the editor in place the link. The user can remove the thumbnail anytime if they don’t want to embed the video in the post.
Behind the scenes, when a valid video link is posted, I create a
VideoEmbed record to embed to the
rich_text_content, basically as an
ActionText::Attachment. I don’t remove the
VideoEmbed record when the user removes the thumbnail, as it breaks the undo mechanism in the Trix editor. This means that there are
VideoEmbed records that are created, but are not embedded to the
Posts if the user decides to not embed it.
I want to create a Rake task that checks for
VideoEmbeds that are not attached to any
Posts, and then delete them. However, I can’t find an easy way to get these records because the
VideoEmbed is not directly attached to the
Post model. Instead, the attachment is linked through the
rich_text_content column of
Have you guys came across a situation like these? I am struggling to find a solution to this in a Rake task. One way is to check and delete the
VideoEmbed records when the user creates or updates the post, is this the best way to do so?
Thanks for your input!