Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you want to read about <strong><a href="http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations" rel="nofollow">Eager Loading Associations</a></strong>.</p> <pre><code>@images = Image.includes(:location).where("images.user_id = ?", 5) </code></pre> <p>This will find <code>Image</code> instances where <code>user_id = 5</code>. It then runs a 2nd query that will <code>JOIN</code> and build the associated <code>Location</code> instance <em>(thats what the <code>.includes(:location)</code> will do for you)</em>.</p> <p>This more closely matches your alternative query, as it does select all columns from <code>images</code> and <code>location</code> tables.</p> <p>You can build an Array based on this containing a hash with only the keys you're interested in through something like this.</p> <pre><code>@hash_object = @images.collect { |i| { caption: i.caption, latitude: i.location.latitude, longitude: i.location.longitude } } </code></pre> <hr> <p>If you want to build this with only a single query, you can use <code>.joins(:location)</code> in combination with <code>.includes(:location)</code></p> <pre><code>Image.joins(:location).includes(:location).where("images.user_id = ?", 5) </code></pre> <p><strong>Important:</strong> This will omit <code>Image</code> instances who have no assoicated <code>Location</code>. You can modify the <code>joins()</code> a bit to help with this, but the above will have this omission.</p> <hr> <p>If you <strong>really</strong> want only specific columns to be selected, read up on <strong><a href="http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields" rel="nofollow">Selecting Specific Columns</a></strong> though there are warnings for the use of this</p> <blockquote> <p>If the select method is used, all the returning objects will be read only.</p> </blockquote> <p>and</p> <blockquote> <p>Be careful because this also means you’re initializing a model object with only the fields that you’ve selected.</p> </blockquote> <hr> <p>In Rails master (not out in 3.2.11) you can pass <em>multiple columns</em> to <a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb#L111" rel="nofollow"><code>.pluck()</code></a> but this appears to only be restricted to a single table <em>(you wouldn't be able to get the <code>locations</code> table's <code>:latitude</code> and <code>:longitude</code> when plucking from <code>Image</code>)</em>. It's good to know about though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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