How to get an array of table names in ruby

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

-C

+3
source share
2 answers

Use what ActiveRecord gives you out of the box:

ActiveRecord::Base.connection.tables
+7
source

I tried

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

and I was told to check my SQL manual. Having done this, I found that

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

worked. Put any actual logic in the block.

+3
source

Source: https://habr.com/ru/post/1712305/


All Articles