Created
July 11, 2013 18:19
-
-
Save hamiltop/5977833 to your computer and use it in GitHub Desktop.
Use logic in select fields
This file contains 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
I would like to go from: | |
Bundle.find_by_sql("select bundles.name, bundles.id, if(bundles.max_count > 100, 1, 0) as big from bundles;") | |
to something more like: | |
Bundle.select([:name,:id,"if(bundles.max_count > 100, 1, 0)"]).all | |
That way I could continue to chain on where clauses and such. | |
Is there a way to go about it? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bundle.find_by_sql("select bundles.name, bundles.id, bitwise_and(sites.support_v2) as full_v2_support join sites on sites.bundle_id = bundles.id group by bundles.id")
There's a much more complex example. In my API endpoint, I need to return whether each bundle only has sites that support v2. Currently I'm doing an eager load of the sites along with bundles and computing full_v2_support in ruby and it is terribly slow and a memory hog.