ruby-on-railsrubyruby-on-rails-4vestal-versions

use vestal versions to access object with version number


I am using vestal version to try and compare two versions of the same model. In order to do this. I need to access the two instances of the model i am trying to compare. I have tried the following code:

@schedule = Schedule.last

@latest_version_model = @schedule.versions.last
@2nd_to_latest_version_of_model = @latest_version_model-1

I know this is wrong. @latest_version_model returns the number version, not the actual object. @ 2nd_to_latest_version_of_model returns an error. How do I access the latest version of the model instance and the 2nd latest version of the model instance?


Solution

  • The method last could take an argument. If you call last(2) you get the 2 latest elements, so doing last(2).first you will get the second latest element.

    @schedule = Schedule.last
    
    @latest_version_model = @schedule.versions.last
    @2nd_to_latest_version_of_model = @schedule.versions.last(2).first