Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Pivots patch rails: named_scope with the :joins can cause table aliasing issues

In order to accomplish some advanced search functionality, we’ve added a lot of named_scopes to our User model. This seems like a good idea, and well within the intended use for named_scopes. Unfortunately, we ran into issues with our :joins. We have a separate User and Profile model, but our advanced search scopes often needed both to make decisions. So we had some scopes that look like this:

class User
  named_scope :verified {
    :conditions => {:email_verified => true}

  named_scope :answered_questions {
    :join => "INNER JOIN profiles ON profiles.user_id = " +
                 "INNER JOIN answers ON answers.profile_id ="

  named_scope :with_name { lambda { |name|
    :join => "INNER JOIN profiles ON profiles.user_id =",
    :conditions => [" LIKE ?", "%#{name}%"]
  } }

Using these named_scopes, we wanted to dynamically construct a finder that would return the results the user was interested, such as: User.verified or User.answered_questions or even User.verified.answered_questions.with_name('Joseph'). The last scope caused issues, unfortunately, with table aliasing. The query ended up joining in the profiles table twice, in exactly the same way without renaming the table, so mysql rejects the query.

The easiest solution to this problem was to use only the hash form for :join clauses, such as :join => :profile. Rails correctly merges multiple consecutive join scopes that use hashes. If you need to use string joins (such as a LEFT JOIN rather than an INNER JOIN) or put a condition directly on your join, then merging goes out the window and the hashed form is immediately converted to a string and all consecutive joins are “merged” by appending them together.

We started by manually aliasing our scopes, but in some cases we were concerned about the amount of duplicate data this was causing in our queries.

We thought about creating a dependency framework for named_scopes, such that you could have a single :profile scope that other scopes were dependent on and it would only ever get added once. This seemed really difficult because of the way the with_scopes are constructed by named_scopes, there was no good place to keep track of these dependencies, and it would still cause problems if you had a manual with_scope, or :join in your find.

Finally we decided that rails fundamentally lacked the capability to deal with duplicate joins, and that we should solve this problem. It seemed a good solution was to allow :join options to take an array of strings as follows:

  named_scope :answered_questions {
    :join => ["INNER JOIN profiles ON profiles.user_id =",
                 "INNER JOIN answers ON answers.profile_id ="]

Now calling User.answered_questions.with_name('Joseph') will create three values in a :join array, two of which are identical and will be uniq’d out. The downside to this approach is that each value in the :join array has to be string identical, or it will not be properly uniq’d.

So if you are mixing hash style :profile joins with string joins of the same table you need to be careful you match the rails generated syntax. We mostly use string style joins to avoid this issue.

Here’s the ticket the we filed and patched:

It has been commited and will roll out with rails 2.2. Since then we have filed two more issues related to :join and :include:

We hope to patch these two as well!

Joseph & David

  1. Back in the timeframe of rails 1.2.3, I wrote a search architecture, albeit less elegant than named_scope, and ran into the *exact* same problem with merging and duplicate joins. Funny, our solutions are identical Рcombining :join arrays and invoking Array#uniq.

    Since that time, I’ve stepped away from the code-base and contemplated a better approach. In my view, ActiveRecord#find and ActiveRecord#named_scope are both missing a more expressive syntax. In addition to :joins => :profile, there should also be :inner_joins => :profile, :outer_joins => :profile, :left_joins => :profile, and all the popular combinations of join types.

    As you can probably envision, merging pains go away, as each join type is merged _only_ with similar join types. Similarly, testing for the duplicate joins much simpler, preferring a single symbol over a long, multi-worded string. My only concern is the potential bugs derived from correctly ordering the joins, but, that is another problem for another day.

    Anyway, great work on pushing those patches back to core. I look forward to benefitting from your work in future rails versions. :)

  2. Adam says:

    I found I was still getting the “Not unique table/alias” problem when trying to use joined columns with named_scope, even with rails 2.2. The record_filter plugin ( is a great enhancement to named_scope that doesn’t have the same problems. record_filter requires rails 2.3.

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *

Share This