This project is archived and is in readonly mode.
[PATCH] Association count with joins/includes
Reported by dstamat (at elctech) | June 8th, 2009 @ 01:09 AM | in 3.x
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.
Comments and changes to this ticket
-
dstamat (at elctech) June 8th, 2009 @ 04:55 AM
Forgot to mention, this is an issue related to named_scope (or scoping in general).
-
dstamat (at elctech) June 12th, 2009 @ 01:45 AM
- no changes were found...
-
dstamat (at elctech) June 12th, 2009 @ 01:45 AM
- 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.
-
dstamat (at elctech) June 13th, 2009 @ 12:36 AM
- no changes were found...
-
dstamat (at elctech) June 13th, 2009 @ 12:36 AM
- 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.
-
dstamat (at elctech) June 13th, 2009 @ 12:57 AM
- no changes were found...
-
dstamat (at elctech) June 13th, 2009 @ 12:57 AM
Sigh... the latest patch cleans up some things I didn't catch in the diff.
-
Santiago Pastorino February 2nd, 2011 @ 04:51 PM
- 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.
-
Santiago Pastorino February 2nd, 2011 @ 04:51 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>