Skip to content

Instantly share code, notes, and snippets.

@gamov
Last active December 18, 2015 08:54
Show Gist options
  • Save gamov/8fe38733012931eb3360 to your computer and use it in GitHub Desktop.
Save gamov/8fe38733012931eb3360 to your computer and use it in GitHub Desktop.
Rails: postgres adapter returns string instead of integer
ris = RequestedItem.currently_requested.joins(:order_request).
group(:item_variant_id).select('requested_items.*, order_request.business_site_id').all
ris.first[:business_site_id].class == String #instead of Fixnum...
#with SQLite with can:
if ris.first[:business_site_id] == DEFAULT_SITE
..
#but with PG, we must:
if ris.first[:business_site_id].try(:to_i) == DEFAULT_SITE
..
#Another example:
RequestedItem.where(id: 1).select(*, 10 AS tq).first.tq.class # => String
# http://stackoverflow.com/questions/12571215/connection-select-value-only-returns-strings-in-postgres-with-pg-gem
#Tried with: Rails 3.0.20 with pg gem 0.17.1 and psql 9.3.5 on mac os x 10.8.5
#Same result with Rails 3.2.22 / pg gem 0.18.4
#Same setup with Rails > 4.0.10 return proper type...
@gamov
Copy link
Author

gamov commented Oct 14, 2014

What's your versions for PostgreSQL, pg gem and rails?

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