Skip to content

Instantly share code, notes, and snippets.

@johnpmitsch
Created May 29, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnpmitsch/83d4b1c568b0aac52c3c to your computer and use it in GitHub Desktop.
Save johnpmitsch/83d4b1c568b0aac52c3c to your computer and use it in GitHub Desktop.
find_by_version
# takes something like key="version" operator="<=" value="1.3"
def self.find_by_version(key, operator, value)
major, minor = value.split('.')
major ||= 0
if /[<>]/ =~ operator
minor ||= 0
query = where("major #{operator} :major OR (major = :major AND minor #{operator} :minor)", :major => major, :minor => minor)
else
if minor.nil?
query = where("major #{operator} (?)", major)
else
query = where("major #{operator} (?) and minor #{operator} (?)", major, minor)
end
end
_, conditions = query.to_sql.split("WHERE")
{ :conditions => conditions }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment