Reputation: 3172
In the admin panel of my app I would like to display all the tables in the database.
The way I am doing this at the moment is getting all the records for each table such as :
@users = User.all
@pages = Page.all
etc.
The I create a table in my view, adding the table headers manually:
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
The I acces all records:
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
etc..
What I would like to do is to get a hash off all tables like this:
@DB is { table_name_1: table_object_1, table_name_2: table_object_2}
Then in my view I would be able to do something like
<% @DB.each do |table| %>
<table>
<% @table.each do |record| %>
<table row>
<%end%>
</table>
<%end%>
I managed to get the table names using this :
@DB = Hash.new
ActiveRecord::Base.connection.tables.each do |table|
next if table.match(/\Aschema_migrations\Z/)
next if table.match(/\Aauthentications\Z/)
klass = table.singularize.camelize.constantize
@DB[klass.name] = klass
end
end
from here: How to list of all the tables defined for the database when using active record?
However I get the error that @DB["User"]
does not have a method each
or more precisely..: undefined method each' for # < Class:0x4ab7af0>
Upvotes: 1
Views: 1622
Reputation: 3172
Nevermind..
Seems I was missing the .all
The correct call is : @DB["User"].all.each
Upvotes: 1