【发布时间】:2015-11-15 17:03:33
【问题描述】:
这是Rails 4 x paper_trail: filter versions by item_id with nested resources 的后续问题。
————
上一个问题的上下文
在我的 Rails 4 应用程序中,我有以下模型:
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
按照documentation 中的说明,我安装了paper_trail gem 来跟踪我的post 模型上的变化,它就像一个魅力。
版本显示在Calendars#Index 视图中,该视图用作用户的仪表板。
————
根据my previous question 的回答,我现在的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
现在的问题是:当用户销毁帖子时,与该帖子关联的所有版本都会消失。
我想要的是,即使在帖子被销毁后也能跟踪它,包括提到它被销毁的版本。
我怎样才能做到这一点?
【问题讨论】:
-
也可以尝试为销毁操作定义
has_paper_trais?我不确定它是如何工作的,但请尝试has_paper_trail :destroy -
我的
post模型中已经有has_paper_trail :on => [:update, :destroy]。问题不在于paper_trail,而在于我猜的查询,因为我可以跟踪所有版本,包括帖子被销毁的时间,拉动@versions = PaperTrail::Version.order('id DESC').limit(10)的时间。
标签: ruby-on-rails ruby-on-rails-4 activerecord nested-resources paper-trail-gem