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.

This project is archived and is in readonly mode.

[PATCH] Association count with joins/includes

#2778
class User < ActiveRecord::Base                                                                                                                                                          
  has_and_belongs_to_many :groups, :include => :admins                                                                                                                                   
end

class Group < ActiveRecord::Base                                                                                                                                                         
  has_and_belongs_to_many :users                                                                                                                                                         
  has_and_belongs_to_many :admins, :class_name => "User", :join_table => "admins_groups", :association_foreign_key => "user_id"                                                          
                                                                                                                                                                                         
  named_scope :sorted_by, :order => "id desc"                                                                                                                                            
  named_scope :recent,    :limit => 10                                                                                                                                                                                                                                                            
end

mysql> desc groups_users;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| group_id | int(11) | YES  |     | NULL    |       | 
| user_id  | int(11) | YES  |     | NULL    |       | 
+----------+---------+------+-----+---------+-------+

mysql> desc groups_users;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| group_id | int(11) | YES  |     | NULL    |       | 
| user_id  | int(11) | YES  |     | NULL    |       | 
+----------+---------+------+-----+---------+-------+

>> @user.groups.sorted_by
=> <#Groups... etc>
>> @user.groups.sorted_by.count
=> 3

>> @user.groups.recent
=> <#Groups... etc>
>> @user.groups.recent.count
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'groups_users.user_id' in 'where clause': SELECT id FROM `groups` WHERE ((`groups_users`.user_id = 1 ) AND (`groups_users`.user_id = 1 ))  LIMIT 10

This only happens when the User has_and_belongs_to_many groups does the include of "admins", and, it happens only on the :limit, and not the :order.

Reported by dstamat (at elctech) · June 8th, 2009 @ 01:09 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. dstamat (at elctech)
    dstamat (at elctech)

    Forgot to mention, this is an issue related to named_scope (or scoping in general).

    June 8th, 2009 @ 04:55 AM

  2. dstamat (at elctech)
  3. dstamat (at elctech)
    dstamat (at elctech)
    • Title changed from HABTM Count, Include with Limit to HABTM and named_scope with include: [w/ Failing Test Attached]

    My explanation sucks, so, see attached for a failing test.

    June 12th, 2009 @ 01:45 AM

  4. dstamat (at elctech)
  5. dstamat (at elctech)
    dstamat (at elctech)
    • Title changed from HABTM and named_scope with include: [w/ Failing Test Attached] to [PATCH] Association count with joins/includes

    Finally got a chance to look at this. See attached for a proposed fix.

    June 13th, 2009 @ 12:36 AM

  6. dstamat (at elctech)
  7. dstamat (at elctech)
    dstamat (at elctech)

    Sigh... the latest patch cleans up some things I didn't catch in the diff.

    June 13th, 2009 @ 12:57 AM

  8. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  9. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:51 PM

  10. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:51 PM

  11. bingbing