Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

This project is archived and is in readonly mode.

Association count with include doesn't alias join table

#302

Let's say we have a has_many :through association:

class Author < ActiveRecord::Base
  has_many :authorships
  has_many :books, :through => :authorships
end

If we are performing a find on books we have the ability to include authorships:

author.books.find(:all, :include => :authorships, :conditions => ["authorships.primary = ?", true])

This works without error. However, if we try to do the same thing for a count:

author.books.count(:include => :authorships, :conditions => ["authorships.primary = ?", true])

We get an error (at least on mysql and sqlite) because it doesn't like it that we are including the authorships table even though it is already included in the join. This leads to an error like this:

ActiveRecord::StatementInvalid: Mysql::Error: Not unique table/alias: 'authorships'

The find call is smart enough to alias the join table with another name, but the count call isn't. The attachment includes a failing test to duplicate this.

Now you may be wondering why add the redundant :include option. In this simple example it's not really necessary, but in more complex examples it can be. Either way I think the count join should behave like the find join.

Seeing this change in behavior makes me think there's a way to DRY up how joins are handled between find and count.

Reported by Ryan Bates · June 2nd, 2008 @ 09:25 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Bernardo Padua
    Bernardo Padua

    IMO there are lots of discrepancies between count and find, this other one bugged be a lot: #348. They should be DRYed up.

    June 7th, 2008 @ 06:11 PM

  2. Edvin Aghanian
    Edvin Aghanian
    • Tag set to activerecord, assertions, has_many, joins, tested

    I've also run into this problem. I'm surprised this is not higher priority since the use of collections, etc, calls on count and immediately produces an error.

    To work around this, I have simply been logging the object, which seems to allow the object set to be used as a collection in the view without calling 'count'.

    July 16th, 2008 @ 02:06 AM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 2.1.1
    • State changed from new to open
    • Assigned user set to Jeremy Kemper

    July 16th, 2008 @ 07:23 AM

  4. mech
    mech

    This problem also occur at will_paginate when it uses count. For example:

    
    @interviews = @job_posting.interviews.paginate(:include => :job_application)
    

    where the JobPosting class is as follow:

    
    class JobPosting < AR
      has_many :job_applications
      has_many :interviews, :through => :job_applications
    end
    

    August 22nd, 2008 @ 02:08 PM

  5. Tarmo Tänav
    Tarmo Tänav

    Possible fix for this (at least seems to satisfy the test and does actually cause the table to be aliased). Included the test from Ryan's patch as it didn't apply cleanly.

    The basic problem was that JoinDependency was only aware of options[:joins] and not the tables that were joined throug scope(:find, :joins) so it didn't know the included association had to be aliased, my fix is simply to generate the full joins string earlier and pass that to JoinDependency. Tests pass, but in reality only around 15 tests hit that code branch and only two actually contain any joins.

    August 26th, 2008 @ 05:04 PM

  6. Repository
    Repository
    • State changed from open to resolved
    • Tag changed from activerecord, assertions, has_many, joins, tested to activerecord, assertions, has_many, joins, patch, tested

    (from [0ed29df6fa704349fd0af0d9521581d6a8eb109c]) Alias included associations if needed when doing a count

    [#302 state:resolved]

    Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...

    August 28th, 2008 @ 07:33 AM

  7. James Le Cuirot
    James Le Cuirot
    • Milestone cleared.
    • Assigned user cleared.
    • Importance changed from to

    I know this ticket is really old but could you guys please take a look at ticket #3339 ? I have come up against a situation where tables are still being joined twice and a stronger solution is required. For count, it could mean removing this existing solution. I'd appreciate your comments.

    September 16th, 2010 @ 04:19 PM

  8. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 07:09 AM