Rails 3使用连接选择SQL查询

我有选择fonction和join的问题。

这是我目前的查询。

@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我试过这个

 @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') 

在我看来,我得到了错误..这是我的看法

  

这是我得到的错误

 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=>#} Extracted source (around line #21): 18: 
19: 20:
21:
22: 23:
24:

谢谢

在我的选择中添加building.id,floors.id和spaces.id后,我尝试显示floors.number和spaces.number

  

给我吗

 [#] 

我不知道为什么我只有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 

我现在遇到的问题是调试给我的。 只有2个领域的建筑物。 是因为Building.select吗? 我怎样才能进入我的选择区域?

谢谢。

看看你的代码:

 Building.select('buildings.name, floors.number, spaces.number)... 

您没有选择构建id ,因此在检索它时Rails有点丢失。

你可以这样做……希望它有用

 @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")