使用活动记录时如何列出为数据库定义的所有表? [英] How to list of all the tables defined for the database when using active record?

查看:20
本文介绍了使用活动记录时如何列出为数据库定义的所有表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用活动记录时获取为数据库定义的所有表的列表?

How do I get a list of all the tables defined for the database when using active record?

推荐答案

调用 ActiveRecord::ConnectionAdapters::SchemaStatements#tables.此方法在 MySQL 适配器中未记录,但在 PostgreSQL 适配器中记录.SQLite/SQLite3 也实现了该方法,但未记录.

Call ActiveRecord::ConnectionAdapters::SchemaStatements#tables. This method is undocumented in the MySQL adapter, but is documented in the PostgreSQL adapter. SQLite/SQLite3 also has the method implemented, but undocumented.

>> ActiveRecord::Base.connection.tables
=> ["accounts", "assets", ...]

参见activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb:21,以及这里的实现:

See activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb:21, as well as the implementations here:

这篇关于使用活动记录时如何列出为数据库定义的所有表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆