Last active
December 17, 2015 08:49
-
-
Save dangerousbeans/5583301 to your computer and use it in GitHub Desktop.
Postgresql GREATEST squeel example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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