我已经阅读了很多关于软删除和存档的内容,并看到了所有的优点和缺点。我仍然对哪种方法最适合我的情况感到困惑。我将使用帖子和评论的概念来看看我是否可以更容易地解释它
Post -> Comments
Post.all
Outside RSS Feeds -> Post -> Comments
RSSFeed.posts (Return the ones that are deleted or not)
帖子被“删除”,但我需要仍然可以从 RSS 提要而不是应用程序管理员访问的帖子。
我听到了很多关于软删除的问题,但我认为它可能对我的应用程序最有意义,并且感觉如果我使用 Archive 那么我将不得不运行多个查询
RSSFeed.posts || RSSFeed.archived_posts
不确定在@$$ 中哪个更有效或更痛苦。想法或例子?我知道这个例子听起来很愚蠢,但试图考虑多种情况,可以用来找出走哪条路。
最佳答案
只需在您的数据库中添加另一列并将其命名为 archivated
.
使用 link_to_if
对于链接:
<%= link_to_unless @post.archivated?, @post.name, post_path(@path) %>
更多的 rails 优点:
应用程序/模型/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
应用程序/模型/archive.rb
class Archive < Post
set_table_name :posts # make this model use the posts table
default_scope where( active: false )
end
现在你可以做这样的事情:
@post = Post.find(some_id)
@post.archivate
Archive.find(some_id) # should return the post you just archivated
关于ruby-on-rails - Rails - 事件和非事件数据的软删除或存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082813/