Rails的4×paper_trail:跟踪版本后,项目被销毁 [英] Rails 4 x paper_trail: keep track of versions after item is destroyed

查看:191
本文介绍了Rails的4×paper_trail:跟踪版本后,项目被销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是<一个跟进的问题href="http://stackoverflow.com/questions/33704552/rails-4-x-paper-trail-filter-versions-by-item-id-with-nested-resources/33707347?noredirect=1#comment55206862_33707347">Rails 4×paper_trail:过滤器版本通过ITEM_ID与嵌套的资源

---

背景从preVIOUS问题

CONTEXT FROM PREVIOUS QUESTION

在我的Rails应用程序4,我有以下型号:

In my Rails 4 app, I have the following models:

class User < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :calendars, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations
end

class Post < ActiveRecord::Base
    belongs_to :calendar
end

我安装了 paper_trail 宝石来跟踪我的发布更改模式在文档的解释和它的作品就像一个魅力。

I installed the paper_trail gem to track changes on my post model as explained in the documentation and it works like a charm.

版本会显示在日历#指数认为,作为一个仪表盘给用户。

Versions are displayed in the Calendars#Index view, which serves as a dashboard to the user.

---

href="http://stackoverflow.com/questions/33704552/rails-4-x-paper-trail-filter-versions-by-item-id-with-nested-resources/33707347?noredirect=1#comment55206862_33707347">my previous问题,我现在有以下code在我的 CalendarsController

Based on the answer to my previous question, I now have the following code in my CalendarsController:

def index
    @user = current_user
    @calendars = @user.calendars.all
    @comments = @user.calendar_comments.order("created_at DESC").limit(20)
    @versions = PaperTrail::Version.where(item_id: Post.where(calendar_id: @calendars.ids)).order('id DESC').limit(10)
end

但问题是现在的:当用户销毁后,所有与此相关联后的版本中消失

Problem is now: when a user destroys a post, all the versions associated with this post disappear.

我想取而代之的,是要保持,即使它被摧毁,其中包括提的是,它被摧毁的版本后的轨道。

What I would like instead, is to keep track of a post even after it is destroyed, including the version mentioning that it was destroyed.

我怎样才能做到这一点?

How can I achieve that?

推荐答案

要实现这一目标最简单的方法是使用PaperTrail的的元数据功能存储在每个帖子的版本记录相应的用户标识。换句话说,denormalise数据只是一点点,使查询变得更加容易。

The simplest way to achieve this is to use PaperTrail's metadata feature to store the appropriate users' ids on each post's version record. In other words, denormalise your data just a little to make querying easier.

class Post < ActiveRecord::Base
  belongs_to :calendar
  has_paper_trail meta: {user_id: :user_ids}  # remember to add a user_id column to the versions table

  def user_ids
    calendar.user_ids  # or whatever you need
  end
end

然后就可以在做你的 CalendarsController

@versions = PaperTrail::Version.where(user_id: current_user.id)
                               .order('id DESC')
                               .limit(10)

这篇关于Rails的4×paper_trail:跟踪版本后,项目被销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆