Skip to main content

Feature Guidance - Linked Posts

General • Asked by Rory Walker

Hey Fellow GoRailers,
I have a feature that I'm implementing and some guidance would be well appreciated.
It's linked posts: every time a posts gets updated, a link is placed on the original report to the (new) updated one.

The updated post is a brand new post (i.e. it has it's own view and URL, etc.) but it is related to other posts.

eg.) So the Product Hunt raises $1 million story, there was the original and say 5 updates. By the time the post is finished and no more updates will be added, the original post should have links to the other 5. Each of the other 5 should have links back to the original and the other stories.

So my thoughts where - using the after_update callback in my Post Controller, then past that updated record into a new Post object and link it via Post_id. I had problems thinking how to create the new post without it updated the original post.
Whilst typing this I remembered that the Ancestry gem may be used, and I could treat each updated message as a child node.


False alarm guys - as I suspected ancestry gem, made this a piece of cake.
https://github.com/stefankroes/ancestry


Ancestry is a great solution to the problem. To do this manually, you may want to instead change the update action in your controller to:

  • Load the old record into a variable
  • Create a new record
  • Then update the old record to link to the new one as a child of it

This way your newest record will always be the most recent and each post can belongs_to it's parent post. In effect, you're just setting old record with a new parent each time you update it.



Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.