Rails - Soft Delete or Archive for active and non active data
archive, ruby-on-rails, soft-delete
Solution
Just add another column in your database and call it `archivated`.
Use `link_to_if` for the links:
<%= link_to_unless @post.archivated?, @post.name, post_path(@path) %>
Some more rails goodness:
app/models/post.rb
class Post < ActiveRecord::Base
default_scope where( active: true )
def archivate
unless self.archivated?
self.archivated = true
self.save
end
end
def dectivate
if self.archivated?
self.archivated = false
self.save
end
end
end
app/models/archive.rb
class Archive < Post
set_table_name :posts # make this model use the posts table
default_scope where( active: false )
end
Now you can do stuff like this:
@post = Post.find(some_id)
@post.archivate
Archive.find(some_id) # should return the post you just archivated
Problem
I have read a lot about soft deletes and archive and saw all the pros and cons. I'm still confused on which approach would work best for my situation. I'll use the concept of posts and comments to see if I can explain it a bit easier ``` Post -> Comments Post.all Outside RSS Feeds -> Post -> Comments RSSFeed.posts (Return the ones that are deleted or not) ``` Post gets "deleted" but I need the posts still accessible from say an RSS Feed but not the admin of the application. I hear a lot of headaches with soft deletes but think it might make the most sense for my application and feel if I use Archive then I would have to run multiple queries ``` RSSFeed.posts || RSSFeed.archived_posts ``` not sure which would be more efficient or more a pain in the @$$. Thoughts or examples? I know this example sounds stupid but trying to think of multiple situations that could be used to figure out which way to go.