How to List All Databases with Activerecord

Rails: How to list database tables/objects using the Rails console?

You are probably seeking:

ActiveRecord::Base.connection.tables

and

ActiveRecord::Base.connection.columns('projects').map(&:name)

You should probably wrap them in shorter syntax inside your .irbrc.

How to list all databases with ActiveRecord

I don't know PostgreSQL, but if there is a SQL query which return this information you can do this:

sql = "select * from ... your sql query here"
records_array = ActiveRecord::Base.connection.execute(sql)

Looks like this is the SQL you need:

SELECT datname FROM pg_database
WHERE datistemplate = false;

how to check the database name that ActiveRecord uses

Rails.configuration.database_configuration

This will give you a hash table with the configurations for each of your environments. E.g. to get your development database name:

Rails.configuration.database_configuration["development"]["database"]

How to list of all the tables defined for the database when using active record?

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", ...]

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

Displaying host and database names in Rails

You can create a rails configuration object and obtain the necessary information from it:

config   = Rails::Configuration.new
host = config.database_configuration[RAILS_ENV]["host"]
database = config.database_configuration[RAILS_ENV]["database"]

See the documentation for Rails::Configuration for details.

How do I view a database with ruby on rails active record?

I assume you're using sqlite3 in development and postgresql in production, which is one interpretation of your statement "database.yml has code for sqlite3 and postgresql".

In development, invoke the sql command line with sqlite3. Then you should get the sql command prompt. Then .help will give you the help menu so you can see what commands are available.

Next you'll want to open the database file, so from the sqlite3 command line, do .open <filename> where <filename> is whatever file is configured in database.yml.

Next you'll probably want to see the tables. So use .tables. From this point, you can use sql to see the contents of the tables like:

select first_name from users;

Or whatever you like, to see the contents.

Edit:
I just realized, by "command line" maybe you meant the Rails console? In this case invoke the rails console with rails c and then you can invoke ActiveRecord commands like User.all{|u| puts u.first_name} etc.

rails 6 has_many relation on multiple databases

For features Googlers, solution is to define relations like this

vacancy model

def user
ActiveRecord::Base.connected_to( database: { reading: :primary } ) do
User.where( id: user_id ).first
end
end

I don't know if this is elegant solution, but it works.

ruby - ActiveRecord multiple databases for the same model

I finally found a gem that I was able to hack to do what I need.

I used Octopus and its sharding capabilities, and I added a simple method to my base controller class (that all my other controllers are based off of) that looks like this:


def on_all_shards(&block)
Octopus.config[:shards].each.collect do |shard, shard_config|
Octopus.using(shard) do
yield
end
end
end

Then in my controllers, whenever I need to aggregate the results of all my shards if just do:


on_all_shards do
Model.first
end

In this case, I would get an array of the first record of this Model/table from each database (1 record / database).

Thank you guys for pointing me to the right direction (ActiveRecord connection/connection_pool management)!



Related Topics



Leave a reply



Submit