Mysql Rails 3 SQL查询选择与联接
我对选择函数和联接有问题 这是我目前的问题Mysql Rails 3 SQL查询选择与联接,mysql,ruby-on-rails,ruby-on-rails-3,Mysql,Ruby On Rails,Ruby On Rails 3,我对选择函数和联接有问题 这是我目前的问题 @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('bu
@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')
在我看来,我犯了错误。。这是我的看法
<% 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:
ActionController::搜索引擎中的路由错误#显示
显示/Users/stephanebaribeau/Sites/cadifice/app/views/search_engine/show.html.erb,其中第21行出现:
没有路由匹配{:action=>“show”,:controller=>“buildings”,:id=>#}
提取的源(第21行附近):
18:
19:
20:
21:
22:
23:
24:
谢谢
在我的选择中添加building.id、floors.id和spaces.id后,我尝试显示floors.number和spaces.number
<%= debug @building %>
给我
[#<Building id: 9, name: "234234">]
[#]
我不知道为什么我只有两个元素,可能是因为选择在一栋建筑上。选择
谢谢
--更新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
@search=Building.select('buildings.id,buildings.slug,floors.id,spaces.id,buildings.name,floors.number,spaces.number').JOIN('internal JOIN floors ON floors.Building\u id=buildings.id internal JOIN spaces ON spaces.floor\u id=floors.id'))
@search=@search.where(“buildings.name类似于“%”#{params[:building_name]}%')如果!参数[:建筑名称]。是否为空?
#@search=@search.where(“buildings.name like?”,params[:building_name])如果!参数[:建筑名称]。是否为空?
如果参数[:空格类型]。是否存在?
@search=@search.where(“spaces.space\u type\u id=?”,参数[:space\u type][:space\u type\u id]),如果!参数[:空格类型][:空格类型id]。是否为空?
结束
@search=@search.where(“floors.min\u net\u rent>={params[:floor\u min\u rent]}”)如果!参数[:楼层最低租金]。空白?
@search=@search.where(“floors.max\u net\u rent查看您的代码:
Building.select('buildings.name, floors.number, spaces.number)...
您没有选择建筑物的id,因此Rails在检索时会丢失一些信息。查看您的代码:
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")
你可以这样做。。。
希望它有用
@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")
你能给我们看一下相关的型号吗?你能给我们看一下相关的型号吗?是的,好的,呵呵,我的部分缺失得很严重,但是如何显示内容呢?我试了b号(没有显示)和b.floors.number,其中我得到了一个未定义的方法错误。谢谢你应该简单地将buildings.id
添加到你的列表中。无需更改你的视图代码:id是必填的是的,我知道,但是如何使用floors.number或spaces.number?我已经添加了id和所有工作。但是我想显示floors.number和spaces.number。谢谢我会提供
的结果,我没有错误消息,没有楼层号,只有空白。我的b.number正确吗?或者我需要其他东西吗?是的,好的,呵呵,我的部分严重缺失,但是如何显示内容?我尝试了b.number(没有显示)和b.floors.number,其中我得到了一个未定义的方法错误。谢谢你应该简单地将buildings.id
添加到你的列表中。无需更改你的视图代码:id是必填的是的,我知道,但是如何使用floors.number或spaces.number?我已经添加了id和所有工作。但是我想显示floors.number和spaces.number。谢谢我将提供
的结果。我没有错误消息,没有楼层。编号,只有空白。我的b.编号正确吗?或者我需要其他东西吗?你能展开此部分来解释它如何回答原始问题吗?你能展开此部分来解释它如何回答原始问题吗?