I have a model called Update
which contains column :version
which is a string.
class Update < ApplicationRecord
validates_presence_of :version
end
The version
column contains string values such as 1.0.1
. Using Gem::Version
, you can compare version numbers like this:
Gem::Version.new('2.1') > Gem::Version.new('2.0.1')
I'd like to do a query where I can get all of the records with version
above a certain number. Is there a way to pass a custom method to .where
so that I can use the version comparator? Such as:
class Update < ApplicationRecord
def version_is_greater_than(that_version)
Gem::Version.new(self.version) > Gem::Version.new(that_version)
end
end
...
Update.where(&:version_is_greater_than)
As you can see, the aforementioned code does not work because I need to pass a version string into that method which I can't seem to do via &:
. Is there a better way to do this so that I can just compare the values within the .where
call? I am trying to stay away from splitting out the major/minor/etc. numbers because I'd just be reinventing the wheel there.