sak*_*ken 3 postgresql activerecord ruby-on-rails rails-console
我想从 Rails 控制台获取当前数据库中的模式列表。目前,我正在执行原始 sql 以通过以下方式获取信息
ActiveRecord::Base.connection.execute("select schema_name from information_schema.schemata")
Run Code Online (Sandbox Code Playgroud)
是否有更正确的方法使用 ActiveRecord 从 Rails 控制台检索此信息?
我已经尝试过以下方法,它对我有用。
data = ActiveRecord::Base.connection.execute('select * from information_schema.schemata')
data.each do |schema|
puts schema['schema_name']
end
Run Code Online (Sandbox Code Playgroud)
它返回模式名称。