title: Activerecord select field from two joined tables
body: <p>In my Rails application, I have 3 models <strong>Order</strong> and <strong>Taxi</strong> and <strong>passenger</strong>. Here is the schema of Order table, where <strong>taxi_id</strong> is the foreign key of Taxi, and <strong>passenger_id</strong> is a foregin key of passenger</p>
  
  <pre><code>  create_table "orders", force: :cascade do |t|
      t.integer  "passenger_id"
      t.integer  "taxi_id"
      t.integer  "price"
      .
      .
      .
  
    end
  </code></pre>
  
  <p>here is Taxi schema:</p>
  
  <pre><code>  create_table "taxis", force: :cascade do |t|
      t.string   "taxiNo"
      t.string   "address"
      .
      .
  
      end
  </code></pre>
  
  <p>and here is my passenger schema</p>
  
  <pre><code>    create_table "passengers", force: :cascade do |t|
      t.string   "name"
      t.string   "address"
      t.string   "destination"
     end
  </code></pre>
  
  <p>address of passenger clears the origin of passenger, and address of Taxi defines the current address of this taxi.</p>
  
  <p><strong>I am trying to run this query:</strong></p>
  
  <pre><code>Taxi.joins(:orders =&gt; :passenger).select('taxis.id, orders.price, passengers.address, passengers.destination').where(:taxis =&gt;{:id =&gt;29 } ).all
  </code></pre>
  
  <p>As a matter of a fact, I am looking for <strong>price, address and destination of passenger</strong> field only, but the result is <code>[#&lt;Taxi id: 29, address: "Narva mnt25, Tartu, Estonia"&gt;]</code>.</p>
  
  <p>Here you can find the result:</p>
  
  <pre><code>SELECT taxis.id, orders.price, passengers.address, passengers.destination FROM "taxis" INNER JOIN "orders" ON "orders"."taxi_id" = "taxis"."id" INNER JOIN "passengers" ON "passengers"."id" = "orders"."passenger_id" WHERE "taxis"."id" = 29
   =&gt; #&lt;ActiveRecord::Relation [#&lt;Taxi id: 29, address: "Narva mnt25, Tartu, Estonia"&gt;]&gt;
  </code></pre>
  
  <p><strong>It gives me Taxi_address meanwhile i asked for Order.price, Passenger.address and Passenger.destination</strong></p>
  
  <p>Important point:</p>
  
  <p>I am sure Taxi_id=29 exists</p>
  
  <pre><code>Taxi.find_by_id(29)
    Taxi Load (0.2ms)  SELECT  "taxis".* FROM "taxis" WHERE "taxis"."id" = ? LIMIT 1  [["id", 29]]
   =&gt; #&lt;Taxi id: 29, taxiNo: "T6", address: "Narva mnt25, Tartu, Estonia", status: "1", avalible_duration: 6, created_at: "2016-01-02 14:44:06", updated_at: "2016-01-02 14:44:06"&gt; 
  </code></pre>
  
  <p>and I am sure that i have the record in my order table</p>
  
  <pre><code>Order.find_by_id(1)
  
    Order Load (0.4ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1  [["id", 1]]
   =&gt; #&lt;Order id: 1, passenger_id: 40, taxi_id: 29, price: 925, created_at: "2016-01-04 04:40:52", updated_at: "2016-01-04 04:40:52"&gt;
  </code></pre>