我在選擇功能和連接時遇到問題。Rails 3使用連接的SQL查詢選擇
這裏是我目前的查詢。
@search = Building.joins('INNER JOIN "floors" ON "floors"."building_id" = "buildings"."id" INNER JOIN "spaces" ON "spaces".floor_id = "floors".id')
,但我想有更多的選擇,我選擇使用floors.number,space.number我在我看來,我得到erreor試過這種
@search = Building.select('buildings.name, floors.number, spaces.number).joins('INNER JOIN "floors" ON "floors"."building_id" = "buildings"."id" INNER JOIN "spaces" ON "spaces".floor_id = "floors".id')
..這是我的看法
<% for b in @building do %>
<div style='width:100%;margin-left:auto;margin-right:auto;margin-top:5px;'>
<div style="float:left;width:50%"><%= b.name %></div>
<div><%= link_to 'view', building_path(b) %></div>
</div>
<% end %>
這是錯誤我得到
ActionController::RoutingError in Search_engine#show
Showing /Users/stephanebaribeau/Sites/cadifice/app/views/search_engine/show.html.erb where line #21 raised:
No route matches {:action=>"show", :controller=>"buildings", :id=>#<Building name: "gigi">}
Extracted source (around line #21):
18: <div style='width:100%;margin-left:auto;margin-right:auto;margin-top:5px;'>
19:
20: <div style="float:left;width:50%"><%= b.name %></div>
21: <div><%= link_to 'view', building_path(b) %></div>
22:
23: </div>
24:
感謝
加入building.id後,floors.id和spaces.id我選擇我嘗試以顯示floors.number和spaces.number
<%= debug @building %>
給我
[#<Building id: 9, name: "234234">]
我不知道爲什麼我只有2個元素,也許這是因爲選擇在Building.select上?
感謝
- 更新14/09
這是我的新控制器
@search = Building.select('buildings.id, buildings.slug, floors.id, spaces.id, buildings.name, floors.number, spaces.number').joins('INNER JOIN floors ON floors.building_id = buildings.id INNER JOIN spaces ON spaces.floor_id = floors.id')
@search = @search.where("buildings.name like '%#{params[:building_name]}%'") if !params[:building_name].blank?
#@search = @search.where("buildings.name like ?", params[:building_name]) if !params[:building_name].blank?
if params[:space_type].present?
@search = @search.where("spaces.space_type_id = ?", params[:space_type][:space_type_id]) if !params[:space_type][:space_type_id].blank?
end
@search = @search.where("floors.min_net_rent >= #{params[:floor_min_rent]}") if !params[:floor_min_rent].blank?
@search = @search.where("floors.max_net_rent <= #{params[:floor_max_rent]}") if !params[:floor_max_rent].blank?
@building = @search
的問題,我現在已經是什麼樣的調試給我。只有兩個領域的建設。是因爲Building.select?我怎樣才能讓我的選擇區域內的所有內容?
謝謝。
你能否告訴我們有關的模型嗎? – David