cwayne2 cwayne2 - 1 month ago 9
Ruby Question

Adding Time and Date to Paper_Trail data

New to Rails, but learning a ton.

I'm using Paper Trail to save versions of my database, focusing on one sum that I want to maintain with each update. The sum is called "xoi_qb".

Within my model, I have defined the data I want to save like this:

def get_xoi_qb
xoi_qb = []
self.versions.each do |version|
unless version.reify.nil?
xoi_qb << version.reify.xoi_qb
end
end
return xoi_qb
end


Within my the html page that I want to display this data, I have this:

<p>Previous XOI</p>
<table summary="XOI Versions">
<tr>
<td><%= @quarterback.versions.map { |version| version.reify.xoi_qb} %></td>
</tr>


This is all working properly.

Alongside the "Previous XOI" data, I want to display the time and date that the "xoi_qb" sum is updated (I'm assuming by using "updated_at", but nothing I've tried is working).

Really appreciate your help.

(Side question: my "XOI" sum is appearing with the brackets (i.e. [745]) on the website...if you can tell me how to get rid of that, it would be awesome.)

Answer

I believe the date and time of the version is stored in the created_at field of the version. Did you try that?

As of your side question: you're basically doing [745].to_s in your view code, i.e. casting an array to a string, and it does what you're asking it to do. Your code looks like your intention was to output all the previous versions. For this to work you would rather iterate the array than convert it to a string. Something like:

<table>
  <% quarterback.versions.each do |version| %>
    <tr>
      <td><%= version.reify.xoi_qb %></td>
    </tr>
  <% end %>
</table>

... And with dates:

<table>
  <% quarterback.versions.each do |version| %>
    <tr>
      <td><%= version.reify.xoi_qb %> dated <%= version.created_at %></td>
    </tr>
  <% end %>
</table>
Comments