3

I would like to get a list of schemas in the current database from the rails console. Currently, I am executing raw sql to get the info via

ActiveRecord::Base.connection.execute("select schema_name from information_schema.schemata")

Is there a more correct way to retrieve this information from the rails console with ActiveRecord?

sakurashinken
  • 3,940
  • 8
  • 34
  • 67

2 Answers2

7

i have tried the following ,it works for me.

data = ActiveRecord::Base.connection.execute('select * from information_schema.schemata')
data.each do |schema|
  puts schema['schema_name']
end

it returns the schema names.

chinna2580
  • 2,065
  • 2
  • 16
  • 30
3

The SQL query you used is very correct. Information schema is part of SQL standard.

ActiveRecord does not have a ready method for this.

So it seems there is no better way.

filiprem
  • 6,721
  • 1
  • 29
  • 42