ruby - How to return a specific key value from an hstore? -
i have psql database contains hstore column, such: column: "a" => "1", "b" => "2", "c" => "3"
in standalone ruby script, correctly accessing database, output specific value "a", returns "1". when attempt loop outputs "a" => "1" instead.
require 'rubygems' require 'pg' require 'open-uri' require'activerecord-postgres-hstore' conn = pgconn.connect("hostname", 1234, '', '', "x", "y", "z") array = conn.exec('select * database') array.each |uri| puts uri['column'] end
the documentation on page http://www.postgresql.org/docs/9.1/static/hstore.html shows can use hstore -> text obtain value, unsure how in ruby.
i have seen question how parse , display hstore key/value in rails , said output gives me both key , value when want value.
i should while database created using rails, not want use script. appreciated.
if want extract values 'a'
keys in column
, let database work:
conn.exec(%q{select column -> 'a' database}).each |_, a| # value in `a`, `_` made column name. end
or, if want work other things:
conn.exec(%q{select other_column, column -> 'a' col_at_a database}).each |row| # @ `row['other_column']` , `row['col_at_a']` ... end
Comments
Post a Comment