Save 36% for Black Friday! Learn more

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Feature Guidance - Linked Posts

Rory Walker asked in General

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.

Reply

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

Reply

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.

Reply

Thanks Chris!

Reply
Join the discussion
Create an account Log in

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

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

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