I'm executing a query with ActiveRecord::Base.connection.exec_query() but keep getting integers as strings like this:
ActiveRecord::Base.connection.exec_query(query)
#<ActiveRecord::Result:0x0000000674e248
@column_types=
{"date"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Date:0x000000067a4c60 @limit=nil, @precision=nil, @scale=nil>,
"property_id"=>#<ActiveRecord::Type::String:0x0000000b316090 @limit=nil, @precision=nil, @scale=nil>,
"property_name"=>#<ActiveRecord::Type::String:0x0000000b316090 @limit=nil, @precision=nil, @scale=nil>,
"subgroup_name"=>#<ActiveRecord::Type::String:0x0000000b316090 @limit=nil, @precision=nil, @scale=nil>,
"channel_id"=>#<ActiveRecord::Type::String:0x0000000b316090 @limit=nil, @precision=nil, @scale=nil>,
"eligible_impressions"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x0000000b316bf8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
"estimated_impressions"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x0000000b316bf8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
"impressions"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x0000000b316bf8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
"estimated_clicks"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x0000000b316bf8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
"imported_clicks"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x0000000b316bf8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
"estimated_costs_eur"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal:0x00000017052ff0 @limit=nil, @precision=nil, @scale=nil>,
"imported_costs_eur"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal:0x00000017052ff0 @limit=nil, @precision=nil, @scale=nil>,
"booking_count"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer:0x0000000b316bf8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,
"booking_revenue_eur"=>#<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal:0x0000000b316658 @limit=nil, @precision=nil, @scale=nil>},
@columns=
["date",
"property_id",
"property_name",
"subgroup_name",
"channel_id",
"eligible_impressions",
"estimated_impressions",
"impressions",
"estimated_clicks",
"imported_clicks",
"estimated_costs_eur",
"imported_costs_eur",
"booking_count",
"booking_revenue_eur"],
@hash_rows=nil,
@rows=[["2017-11-13", "CHILD", "childname", "unitname", "Google", nil, nil, "300", "0", nil, "0.00000", nil, "0", "0.00"]]>
Can anyone tell me how to get the correct types, as this information is already contained in the result object?
The database is Postgres - I remember that I didn't have this problem with mysql. Rails is still 4.2.4
On Rails 4.2 you can use cast_values from ActiveRecord::Result
ActiveRecord::Base.connection.exec_query(query).cast_values
Rails 5 will already return casted values as highlighted by the documentation:
# Get the record values of the result:
result.rows
# => [[1, "title_1", "body_1"],
[2, "title_2", "body_2"],
...
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With