列出包括关联的所有paper_trail版本的正确方法是什么? [英] What's the right way to list all paper_trail versions including associations?

查看:150
本文介绍了列出包括关联的所有paper_trail版本的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 paper_trail宝石的问题.

仅关联更改时,不会为主模型创建版本记录.那么,列出特定记录及其关联的所有版本的正确方法是什么?

When only associations change, a version record won't be created for the main model. So what's the right way to list all versions for a certain record including its associations?

我应该查询类似的东西吗? (不好的地方是此SQL查询可能会很长且性能低下.)

Should I query something like this? (The bad point is this SQL query might be long and low performance.)

f = "(item_type = 'Place' AND item_id = ?) OR (item_type = 'PlaceName' AND item_id IN (?))"
PaperTrail::Version.where(f, @place.id, @place.names.map { |n| n.id }) 

还是仅当关联更改时才创建版本记录?我认为@DavidHam尝试了同样的事情,并问了一个类似的问题,但尚未有人回答.

Or should I create a version record when only associations changed? I think @DavidHam tried the same thing and asked a similar question but nobody has answered it yet.

推荐答案

因此,我找到了一种方法来执行此操作,但是它并不十分漂亮,并且每次更改关联时都不会创建新版本.但是,它确实为您提供了一种按时间顺序检索版本的有效方法,因此您可以查看关联更改之前/之后的版本.

So, I sort of found a way to do this, but it's not exactly pretty and it doesn't create a new version everytime an association is changed. It does, however, give you an efficient way to retrieve the versions chronologically so you can see what the version looked like before/after association changes.

首先,在给定该模型的ID的情况下,我检索了所有Association版本的ID:

First, I retrieve all the ids for for the asscoiation versions given the id of that model:

def associations_version_ids(item_id=nil)
  if !item_id.nil?
    ids = PaperTrail::VersionAssociation.where(foreign_key_id: item_id, foreign_key_name: 'item_id').select(:version_id)

    return ids
  else
    ids = PaperTrail::VersionAssociation.where(foreign_key_name: 'item_id').select(:version_id)

    return ids
  end
end

然后,我使用此函数中的VersionAssociation ID将所有版本汇总在一起.它将返回一个按时间顺序排列的PaperTrail :: Version的数组.因此,该信息对于活动日志等很有用.以这种方式将版本及其关联重新组合在一起非常简单:

Then I get all versions together using the VersionAssociation ids from this function. It will return a chronological array of PaperTrail::Version's. So the information is useful for an activity log, etc. And it's pretty simple to piece back together a version and its associations this way:

def all_versions
  if !@item_id.nil?
    association_version_ids = associations_version_ids(@item_id)
    all_versions = PaperTrail::Version
                      .where("(item_type = ? AND item_id = ?) OR id IN (?)", 'Item', @item_id, association_version_ids)
                      .where("object_changes IS NOT NULL")
                      .order(created_at: :desc)

    return all_versions
  else
    assocation_ids = associations_version_ids
    all_versions = PaperTrail::Version
                      .where("item_type = ? OR id IN (?)", 'Item', association_ids)
                      .where("object_changes IS NOT NULL")
                      .order(created_at: :desc)

    return all_versions
  end
end

同样,这不是一个完美的答案,因为每次发生更改时都不会有新版本,但这是可管理的.

Again, not a perfect answer since there isn't a new version everytime there's a change, but it's manageable.

这篇关于列出包括关联的所有paper_trail版本的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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