0
我遇到了記錄未從數據庫返回的問題。沒有錯誤被拋出,但也許我錯過了簡單的東西。我將我的數據庫yml設置爲開發,測試和生產到同一個東西,以確保我的目標是數據庫。簡單導軌3查詢未加載數據
直接從數據庫查詢返回3個機構。
型號 - institution.rb
class Institution < ActiveRecord::Base
# attr_accessible :title, :body
has_many :childinstitutions, :class_name => "Institution",
:foreign_key => "parentinstitution_id"
belongs_to :parentinstitution, :class_name => "Institution"
def self.search(term)
if term
Institution.where('institutionname LIKE ?', :term).all
else
Institution.all
end
end
end
控制器 - institutions_controller.rb
class InstitutionsController < ApplicationController
def index
@institutions = Institution.search(params[:term])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @institutions }
end
end
def show
@institution = Institution.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @institution }
end
end
def new
@institution = Institution.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @institution }
end
end
end
視圖 - 機構/ index.html.erb
<h1>Listing Institutions</h1>
<table>
<tr>
<th>Id</th>
<th><% @institutions.length %> records</th>
</tr>
<% for institution in @institutions %>
<tr>
<td><% institution.id %></td>
<td></td>
</tr>
<% end %>
</table>
<br />
結果 真的很大上市機構和Id記錄在下一行。