Skip to content

Instantly share code, notes, and snippets.

@hamiltop
Created July 11, 2013 18:19
Show Gist options
  • Save hamiltop/5977833 to your computer and use it in GitHub Desktop.
Save hamiltop/5977833 to your computer and use it in GitHub Desktop.
Use logic in select fields
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?
@hamiltop
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment