views:

47

answers:

0

I am using ActiveRecord and want to average a generic table column that's typed as a string (char), but holds numbers:

| key | value |
| "square_footage" | "142555" |

How do I write this so Postgres doesn't give me this error:

PGError: ERROR: function avg(character varying) does not exist LINE 1: SELECT avg("settings".value) AS avg_value FROM "settings" WH... HINT: No function matches the given name and argument types. You might need to add explicit type casts. : SELECT avg("settings".value) AS avg_value FROM "settings" WHERE ("settings"."key" = 'square_footage')

I'm using that in ActiveRecord like this:

Setting.average("value", :select => 'CAST("settings".value AS INTEGER)', :conditions => {:key => "suite_number"})

It works fine in MySQL, not on Heroku.

Thanks