This Gem provides a way to work with semantic version numbers in ActiveRecord models.
Add the following line to your Gemfile and run the bundle
command to install it.
gem "versionable"
Call version_field
from within an ActiveRecord model class and pass the name of the field to be used as a version number. This field must be of type Float
.
class Release < ActiveRecord::Base version_field :version end
Versionable now will setup a wrapper around the version
field.
- Assigning and retrieving can be done as if the version field were a string:
release.version = '1.2.34' # => "1.2.34" release.version # => "1.2.34"
- It is possible to access the value as it’s stored in the database through
raw_#{version_field_name}
.
release.raw_version # => 0.01002034
Versionable internally stores the version number as float using the following simple rule:
Version X.Y.Z is saved internally as the float value x.xxyyyzzz.
So version 1.2.3 is stored as 0.01002003.
This allows to do SQL searches using the ordering clause.