Find the version in which a particular attribute has changed in the folder

I use the Papertrail gem in my project and have searched a lot to try and find the following.

What I would like to do is find the version of my object where a particular attribute has become a specific value, i.e.

object.versions.where(attribute: "value")

Does anyone know if this is even possible with Papertrail?

+9
source share
3 answers

Well, I came to the conclusion that this is not possible out of the box in paper_trail.

As a result, I updated one of the defined attributes, saving the flag in a separate metadata column in the Versions table.

, , .

, - .

+5

, attribute value ( , , 1):

object.versions.
       map(&:object_changes).
       map{|a| YAML::load a}.
       select{|a| (a[:attribute] || [])[1]=='value'}

:

[{"attribute"=>[nil, "value"],
  "updated_at"=>[2015-01-02 12:20:36 UTC, 2015-01-02 12:20:56 UTC]}]

updated_at[1] , attribute value.

.

+8

Here is another option for those who do not want to use a separate metadata column:

def version_where_attribute_changed_to(attribute_name, attribute_value)
  versions.select do |version|
    object_changes = YAML.load version.object_changes
    (object_changes[attribute_name.to_s] || [])[1] == attribute_value
  end
end
+4
source

Source: https://habr.com/ru/post/1544150/


All Articles