Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an array of table names in ruby

I'm trying to get the output of the query 'SHOW TABLES FROM database_name' into an array using the ActiveRecord database connection. I can't seem to figure out how to do this. can someone please enlighten me?

-C

like image 848
Chris Drappier Avatar asked Nov 21 '25 08:11

Chris Drappier


2 Answers

Use what ActiveRecord gives you out of the box:

ActiveRecord::Base.connection.tables
like image 145
François Beausoleil Avatar answered Nov 23 '25 23:11

François Beausoleil


I tried

ActiveRecord::Base.connection.execute("DESCRIBE TABLE table_name")

and was told to check my SQL manual. Doing so, I found that

ActiveRecord::Base.connection.execute("DESCRIBE table_name").each{|r| p r }

worked. Put whatever actual logic you need in the block.

like image 39
Ben Hughes Avatar answered Nov 24 '25 00:11

Ben Hughes



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!