Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 SQL query select with joins
    text
    copied!<p>I have a problem with select fonction and joins.</p> <p>here is my current query.</p> <pre><code>@search = Building.joins('INNER JOIN "floors" ON "floors"."building_id" = "buildings"."id" INNER JOIN "spaces" ON "spaces".floor_id = "floors".id') </code></pre> <p>but i want to have more option in my select to use floors.number, space.number i tried this</p> <pre><code>@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') </code></pre> <p>in my view i got erreor.. here is my view</p> <pre><code>&lt;% for b in @building do %&gt; &lt;div style='width:100%;margin-left:auto;margin-right:auto;margin-top:5px;'&gt; &lt;div style="float:left;width:50%"&gt;&lt;%= b.name %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= link_to 'view', building_path(b) %&gt;&lt;/div&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>this is the error i get</p> <pre><code>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=&gt;"show", :controller=&gt;"buildings", :id=&gt;#&lt;Building name: "gigi"&gt;} Extracted source (around line #21): 18: &lt;div style='width:100%;margin-left:auto;margin-right:auto;margin-top:5px;'&gt; 19: 20: &lt;div style="float:left;width:50%"&gt;&lt;%= b.name %&gt;&lt;/div&gt; 21: &lt;div&gt;&lt;%= link_to 'view', building_path(b) %&gt;&lt;/div&gt; 22: 23: &lt;/div&gt; 24: </code></pre> <p>thanks</p> <p>After adding building.id, floors.id and spaces.id on my select i try to show the floors.number and spaces.number</p> <pre><code>&lt;%= debug @building %&gt; </code></pre> <p>give me </p> <pre><code>[#&lt;Building id: 9, name: "234234"&gt;] </code></pre> <p>i dont know why i have only 2 elements, maybe it's because the select are on a Building.select?</p> <p>thanks</p> <p>-- Update 14/09</p> <p>Here is my new controller</p> <pre><code> @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 &gt;= #{params[:floor_min_rent]}") if !params[:floor_min_rent].blank? @search = @search.where("floors.max_net_rent &lt;= #{params[:floor_max_rent]}") if !params[:floor_max_rent].blank? @building = @search </code></pre> <p>The problem i have now is what debug give me. only 2 fields from the building. Is it because of the Building.select? How can i get all inside my selectfield?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload