Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate over all values of a pg_conn query in Ruby

I have a ruby script where I need to retrieve all values from a database and do stuff with each one retrieved. Currently I am hard coding each retrieval, but this only works if there are 3 values coming back. How can I iterate over this? Below is my code:

  require 'pg'
  pg_conn = PGconn.connect(host = "main-pg-db-super.center.com", port = 6433, options = '', tty = '', 
                        dbname = "master_data", login = "user", password = "password")

  all_children = pg_conn.exec("SELECT id FROM pt.tests WHERE parent = '1';") # will return 3 results
  puts all_children[0]['id']
  puts all_children[1]['id']
  puts all_children[2]['id']
like image 412
Horse Voice Avatar asked Oct 15 '25 19:10

Horse Voice


1 Answers

all_children.each do |child|
  puts child['id']
end

Does this not work?

like image 99
Reck Avatar answered Oct 18 '25 15:10

Reck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!