Skip to content

Instantly share code, notes, and snippets.

View oboxodo's full-sized avatar

Diego Algorta oboxodo

View GitHub Profile
@oboxodo
oboxodo / README.md
Last active September 15, 2024 15:31

Avoid has_many/one + belongs_to circular dependency

This is based on the use of Shopify's packwerk.

Asuming we have a posting pack with a Post model and a commenting pack with a Comment model, and we want the commenting pack to have a declared dependency on the posting pack, then here's a pattern to avoid the has_many :comments usual declaration on the Post model while still being able to have very similar functionality to the dependent option to react/restrict when a post is destroyed.

If you need to fetch a post's comments, you can simply use Comments.where(post: @post) instead of the