This project is archived and is in readonly mode.

#2778 ✓stale
dstamat (at elctech)

[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

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>

People watching this ticket

Pages