Skip to content

Instantly share code, notes, and snippets.

@dangerousbeans
Last active December 17, 2015 08:49
Show Gist options
  • Save dangerousbeans/5583301 to your computer and use it in GitHub Desktop.
Save dangerousbeans/5583301 to your computer and use it in GitHub Desktop.
Postgresql GREATEST squeel example
require 'squeel'
# Say a Farm has two integers to keep track of its stock and a constant to define what the minimum number of animals is
class Farm < ActiveRecord::Base
MIN_ANIMALS = 10
attr_accessible :kitten_count, :unicorn_count
end
# Now I want to bring back the largest count of animals for each farm, with the minimum value being MIN_ANIMALS incase both are below 10
Farm.select{ op('GREATEST', [kitten_count, unicorn_count, MIN_ANIMALS).as{ total_animal_count } }
# Does not work :C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment