Cameron
Cameron

Reputation: 28803

Get next and previous versions for a version id using paper_trail

I'm using the paper_trail gem to version my Pages model.

I have a method called version which takes a page_id and version_id.

def version
  page = Page.where(id: params[:page_id]).first
  @version = page.versions.where(id: params[:version_id]).first
  @previous_version = @version.previous_version rescue nil
  @next_version = @version.next_version rescue nil
end

What I want to do is get the next and previous versions to pass them to my view. However I can only access the current version. @previous_version and @next_version are always nil even though I have next and previous versions. It seems it doesn't know what the methods previous_version and next_version are.

Upvotes: 1

Views: 467

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

Version object has next and previous methods, whereas the versioned object (in your case it's @page) has methods next_version and previous_version.

So:

@page.previous_version
#=> returns previous version of @page object

whereas

@page.versions.previous
#=> returns a previous version object

Upvotes: 1

Related Questions