Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paper trail retrieve object

I am trying to retrieve the object that paper trail has versioned. However when I use .reify, it retrieves the previous version. not the most recent.

for example

Controller

@line_item_versions = PaperTrail::Version.where(item_type: 'LineItem', secondary_id: params[:id]).order('created_at ASC')

View

<% @line_item_versions.each_with_index do |line_item, index| %>
<% object = line_item.reify(options = {}) %>
          <% if object %>
                <b><%= object.name %></b> : <%= object.quantity %>
              </font>
          <% end %>

object = line_item.last.reify(options = {}) or object = line_item.next_version.reify(options = {}) both return undefined method errors.

How do I retrieve the appropriate object so that I can interact and display specific information?

like image 319
user3754124 Avatar asked Nov 24 '25 15:11

user3754124


1 Answers

This is because PaperTrail stores the object as it was before the change, thus, when you reify it, it will show its previous state, if you'd like to get the name as it was after the change, you can add the object_change column to your versions table, then call the changeset if your needed object has changed if it has, print the updated field, if its not, print the reified object field, like this:

<%= line_item.changeset[:name].nil? object.name : line_item.changeset[:name][1] %></b> : <%= line_item.changeset[:quantity].nil? object.quantity : line_item.changeset[:quantity][1] %>
like image 50
Marcelo Risoli Avatar answered Nov 27 '25 05:11

Marcelo Risoli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!