【问题标题】:Rails 3 SQL query select with joinsRails 3 SQL查询选择与连接
【发布时间】:2011-09-12 19:19:42
【问题描述】:

我在选择函数和连接时遇到问题。

这是我当前的查询。

@search = Building.joins('INNER JOIN "floors" ON "floors"."building_id" = "buildings"."id" INNER JOIN "spaces" ON "spaces".floor_id = "floors".id')

但我想在我的选择中有更多选项来使用 floor.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: 

谢谢

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

<%= debug @building %>

给我

[#<Building id: 9, name: "234234">]

我不知道为什么我只有 2 个元素,可能是因为选择在 Building.select 上?

谢谢

-- 2009 年 14 月更新

这是我的新控制器

  @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 吗?我怎样才能在我的选择字段中获取所有内容?

谢谢。

【问题讨论】:

  • 能否给我们展示一下相关型号?

标签: mysql ruby-on-rails ruby-on-rails-3


【解决方案1】:

看看你的代码:

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

您没有选择建筑物id,所以当需要检索它时,Rails 会有点丢失。

【讨论】:

  • 是的,好的,呵呵,我的部分很糟糕,但是如何显示内容?我尝试了 b.number (nothing show) 和 b.floors.number ,我得到一个未定义的方法错误。谢谢
  • 您只需将buildings.id 添加到您的列表中即可。无需更改您的视图代码:id 是强制性的
  • 是的,我知道,但是如何使用 floors.number 或 spaces.number?我已经添加了 id 和所有作品。但我想显示楼层数和空间数。谢谢
  • 以上都提供&lt;%= debug @search %&gt;的结果
  • 我没有错误信息,没有楼层数,只有空白。我的 b.number 是否正确?还是我需要别的东西?
【解决方案2】:

你可以这样做... 希望有用

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

【讨论】:

  • 您能否扩展它以解释它如何回答原始问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
相关资源
最近更新 更多