使用活动记录时如何列出为数据库定义的所有表? [英] How to list of all the tables defined for the database when using active record?
问题描述
如何在使用活动记录时获取为数据库定义的所有表的列表?
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:
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:412
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:615
activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:176
这篇关于使用活动记录时如何列出为数据库定义的所有表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!