Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveRecord 3.2 vs. 3.0 Uniq Method Ignored in Join Statement
    primarykey
    data
    text
    <p>About two weeks ago I updated my Rails app from 3.0 to 3.2 (just in time for 4! Sigh.). Anyway, I've just noticed something odd — a query that returned correctly-ordered results in the old version is returning consistently-incorrect results in 3.2.</p> <p>Here's the query:</p> <pre><code>@sessions = user.email_sessions.joins(:email_stats) .where('email_stats.event_type = "open"') .order('email_stats.created_at DESC') .uniq </code></pre> <p>This query in Rails 3.2 returns these timestamps:</p> <pre><code>2013-06-18 12:02:05 -0400 2013-06-18 11:24:22 -0400 2013-06-18 10:41:52 -0400 2013-06-18 12:09:42 -0400 2013-06-18 10:02:26 -0400 2013-06-18 10:24:40 -0400 2013-06-18 08:59:19 -0400 2013-06-18 08:59:19 -0400 2013-06-18 08:51:39 -0400 2013-06-14 15:47:28 -0400 </code></pre> <p>Whereas in 3.0, I get this:</p> <pre><code>2013-06-18 12:09:42 -0400 2013-06-18 12:03:23 -0400 2013-06-18 12:02:05 -0400 2013-06-18 11:24:22 -0400 2013-06-18 10:41:52 -0400 2013-06-18 10:24:40 -0400 2013-06-18 10:02:26 -0400 2013-06-18 08:59:19 -0400 2013-06-18 08:59:19 -0400 2013-06-18 08:51:39 -0400 </code></pre> <p>That's good! Rails 3.2 is bad! Can anyone shed some light on this issue?</p> <p><strong>Update:</strong> It just occurred to me to check Rails 3.1, which I slid through briefly on my way to 3.2. I checked out the latest commit of that branch, and the dates are good there. It's only on 3.2 that this problem occurs.</p> <p><strong>Update 2:</strong> Thanks to Dhurv's question below, I've gotten the raw SQL being generated in both versions of Rails. I used the .to_sql method on the first, and I pulled the latter from the log. <strong>The difference: the .uniq method is only there in 3.2</strong> Why?</p> <p>Rails 3.2</p> <blockquote> <p>SELECT DISTINCT <code>email_sessions</code>.* FROM <code>email_sessions</code> INNER JOIN <code>email_stats</code> ON <code>email_stats</code>.<code>email_session_id</code> = <code>email_sessions</code>.<code>id</code> WHERE <code>email_sessions</code>.<code>user_id</code> = 374 AND (email_stats.event_type = "open") ORDER BY email_stats.created_at DESC</p> </blockquote> <p>Rails 3.0</p> <blockquote> <p>SELECT <code>email_sessions</code>.* FROM <code>email_sessions</code> INNER JOIN <code>email_stats</code> ON <code>email_stats</code>.<code>email_session_id</code> = <code>email_sessions</code>.<code>id</code> WHERE (<code>email_sessions</code>.user_id = 374) AND (email_stats.event_type = "open") ORDER BY email_stats.created_at DESC</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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. 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