Reputation: 5354
I'm a Codeigniter developer and for one of my project I need to use Rails instead of CI. I have a database with more than 30 tables which might be added more tables in the future. My task is to get all those tables and show them each individually in a form of bar chart.
All I know about rails is using controller and model with active records to get the datas. I don't want to create individual model for each of the table. Hence, I was wondering is there any better solution such as asking rails to look for available tables inside the database, and based on the requested URI, present the data from table.
Upvotes: 2
Views: 172
Reputation: 24793
With active record you can run sql directly using:
ActiveRecord::Base.connection.select_all(sql)
So, if I understand your question, you want to get a list of tables in the DB, which on postgres could be done like this:
sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';"
table_names = ActiveRecord::Base.connection.select_all(sql)
table_names.to_a
# => [{"table_name"=>"orders"},
# {"table_name"=>"plans"},
# {"table_name"=>"products"},
# {"table_name"=>"services"},
# {"table_name"=>"users"},
# {"table_name"=>"schema_migrations"}]
Upvotes: 2