rails/rails — Issue #4486: Rails 3.2 -- not type casting correctly from postgresql view
Author: joevandyk
Created: 2012-01-17 01:56:27+00:00
State: open

On postgresql 9.1. Note that the `quantity` column is being converted into a string, not an integer.

```
# products is a postgresql table
class Product < ActiveRecord::Base
....
end

# buyable_products is a postgresql view
class BuyableProduct < ActiveRecord::Base
...
end

tanga_dev=# \d buyable_products
View "public.buyable_products"
Column | Type | Modifiers
---------------------+-----------------------------+-----------
id | integer |
name | character varying(255) |
normal_price | numeric |
stu_price | numeric |
active_user_price | numeric |
second_chance_price | numeric |
shipping_price | numeric |
stu_only | boolean |
msrp | numeric |
fake_sold_out | boolean |
category_id | integer |
start_sale_date | timestamp without time zone |
priority_number | integer |
description | text |
subtitle | character varying(255) |
search_tags | text |
quantity | integer |
slug | text |

# It's an integer here!
BuyableProduct.columns[-2]
=> #

# It's a string here!
1.9.1 :002 > BuyableProduct.last.read_attribute :quantity
BuyableProduct Load (3.5ms) SELECT "buyable_products".* FROM "buyable_products" ORDER BY "buyable_products"."id" DESC LIMIT 1

# It's a string here!
BuyableProduct.last
BuyableProduct Load (2.0ms) SELECT "buyable_products".* FROM "buyable_products" ORDER BY "buyable_products"."id" DESC LIMIT 1
=> #, stu_price: nil, active_user_price: nil, second_chance_price: nil, shipping_price: "0.0", stu_only: false, msrp: #, fake_sold_out: false, category_id: 22, start_sale_date: "2012-01-16 03:00:00", priority_number: nil, description: "Lolshirt t-shirt description", subtitle: nil, search_tags: "", quantity: "20", slug: "lolshirt-20">
=> "20"

```