Question

I'm using the paper_trail gem in my Rails 4 application and I want to set a default sort order. papertrail doesn't have a model, only a controller and an initializer.

Where can I put it? Do I have to create a model for it?

default_scope order('created_at DESC')
Was it helpful?

Solution 2

Well I found that I could just use reverse in my each do block.

versions.reverse.each

Since I need nothing more complex than that I guess it will do for now.

OTHER TIPS

There's also the reorder command.

http://apidock.com/rails/ActiveRecord/QueryMethods/reorder

versions.reorder('created_at DESC')

It will replace any prior ordering, thus ignoring Papertrail's pre-defined order.

The correct way would be to remove the previous sorting via unscope and then set it however you want:

http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-unscope

For example:

somethings.version.merge(PaperTrail::Version.unscope(:order)).order('created_at DESC')

This would give you a proper Query with the right sorting

find version.rb in paper_trail gem. directory on my computer shown below. add:

scope :by_date_desc, lambda { unscope(:order).order('created_at DESC') }

unscope necessary to remove prior order in chain (thank you maxigs). use your favorite scope name of course. worked for me.

C:\Ruby200\lib\ruby\gems\2.0.0\gems\paper_trail-4.0.0.beta2\lib\paper_trail\frameworks\active_record\models\paper_trail\version.rb

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top