1
在我的應用的管理面板中,我想顯示數據庫中的所有表。在Rails控制器中獲取數據庫中的所有表
的方式我這樣做此刻正讓所有的記錄爲每個表,如:
@users = User.all
@pages = Page.all
etc.
的我在視圖中創建一個表,手動添加表頭:
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
的存取權限我的所有記錄:
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
etc..
我想這樣做是爲了得到一個散列關閉所有T ABLES這樣的:
@DB is { table_name_1: table_object_1, table_name_2: table_object_2}
然後在我看來,我將能夠像做
<% @DB.each do |table| %>
<table>
<% @table.each do |record| %>
<table row>
<%end%>
</table>
<%end%>
我設法用它來獲取表名:
@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
從這裏: How to list of all the tables defined for the database when using active record?
但是我得到的錯誤@DB["User"]
沒有一個方法each
或者更確切地說..:undefined method each' for # < Class:0x4ab7af0>
你可以用'rails_admin','active_admin'或我的簡單庫'bareback'執行此任務,請查看https://github.com/nielsbuus/bareback – 2014-11-23 15:19:10