Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: ActiveRecord query with :includes only returns results that have related values in included table?
    primarykey
    data
    text
    <p>I've got a model (a <code>Feature</code>) that can have many <code>Assets</code>. These <code>Assets</code> each have an <code>issue_date</code>. I'm struggling with what seems like a simple ActiveRecord query to <strong>find all <code>Features</code> and their <code>Assets</code> with an <code>issue_date</code> of tomorrow, regardless of if there are <code>Assets</code> or not</strong> — preferably with one query.</p> <p>Here's my query right now.</p> <pre><code>Feature.includes(:assets).where(:assets =&gt; { :issue_date =&gt; Date.tomorrow }) </code></pre> <p>Unfortunately, this returns only the <code>Features</code> that have <code>Assets</code> with an <code>issue_date</code> of tomorrow. Even stranger, the generated SQL looks like this (tomorrow's obviously the 19th).</p> <pre><code>SELECT `features`.* FROM `features` WHERE `assets`.`issue_date` = '2011-08-19' </code></pre> <p>Shouldn't this have an <code>LEFT JOIN</code> in there somewhere? That's the sort of thing I'm going for. Using <code>joins</code> instead of <code>includes</code> does an <code>INNER JOIN</code>, but that's not what I want. Strangely enough, it seems like I'm getting an <code>INNER JOIN</code>-type of behavior. When I run that <code>includes</code> query above, the actual SQL that's spit out looks something like this...</p> <pre><code>SELECT `features`.`id` AS t0_r0, `features`.`property_id` AS t0_r1, // every other column from features truncated for sanity `assets`.`feature_id` AS t1_r1, `assets`.`asset_type` AS t1_r2, // all other asset columns truncated for sanity FROM `features` LEFT OUTER JOIN `assets` ON `assets`.`feature_id` = `features`.`id` WHERE `assets`.`issue_date` = '2011-08-19' </code></pre> <p>Which <strong>looks</strong> like it should work right but it doesn't. I get <em>only</em> the <code>Features</code> that have <code>Assets</code> with an <code>issue_date</code> of tomorrow. Any idea what I'm doing wrong?</p> <p>I've tried the older, Rails v2 way of doing it…</p> <pre><code>Feature.find(:all, :include =&gt; :assets, :conditions =&gt; ['assets.issue_date = ?', Date.tomorrow]) </code></pre> <p>Which gives me the same results. There's one <code>Feature</code> I know that doesn't have any <code>Assets</code> for tomorrow, and it's not in that list.</p> <p>I've also poked around and found similar questions, but I couldn't seem to find one that explained this opposite behavior I'm seeing.</p> <p><strong>Edit</strong>: I'm <em>so</em> close. This gets me all the <code>Feature</code> objects.</p> <pre><code>Feature.joins("LEFT OUTER JOIN assets on assets.feature_id = feature.id AND asset.issue_date = #{Date.tomorrow}") </code></pre> <p>It does <strong>not</strong>, however, get me the matching <code>Assets</code> bundled into the object. With <code>feature</code> as a returned item in the query, <code>feature.assets</code> makes another call to the database, which I don't want. I want <code>feature.assets</code> to return only those I've specified in that <code>LEFT OUTER JOIN</code> call. What else do I need to do to my query?</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.
 

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