所有数据库中定义的表如何使用活动记录的时候上市?如何使用、库中、定义、时候

2023-09-08 15:48:32 作者:高冷女王

我如何得到所有数据库中定义的表的列表,使用活动记录时?

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