Question

I have thought hard on this, and searched a lot, but it seems there is not many people who have encountered this situation. With Rails 4, what is a straight forward way to have a published/draft setup with has_many associations and some degree of versioning?

Let's say we have the following setup:

class Parent < ActiveRecord::Base
    has_many :children
    has_many :other_children
end

class Child < ActiveRecord::Base
    belongs_to :parent, inverse_of: :children
end

class OtherChild < ActiveRecord::Base
    belongs_to :parent, inverse_of: :other_children
end

What I want to achieve is the following:

1) Keep a published version of parent AND it's children together. Thus there is a fixed state published where the children also stays intact as they were when they were published.

2) Keep a draft version of parent and children where they can be modified individually (and children can be destroyed).

3) Keep a version history for the published versions of Parent/Children.

I have been checking out the versioning gems like paper_trail, but they don't cut it mainly due to my requirement of the children also staying intact and an easy way to get the published version. It seems like a lot of extra work to make it work, and it might be faster to build some more specific setup.

Thanks for the help!

Was it helpful?

Solution

The way I did it was to just keep the draft and published versions in the same tables, and clone the objects instead with the deep_cloneable gem. It's an amazing gem.

The published version has a field published_at set, while the draft version doesn't. This was far the smoothest approach I could find.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top