Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9175 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Calling count() on a has_many association ignores explicit :include option

Reported by: danger Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: tiny patch needs_review
Cc:

Description

This works:

@author.posts.find(:all, :include => :comments, :conditions => 'comments.id is null')

This generates invalid SQL:

@author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')

It's a minor fix (patch included). We just need to not clobber the :include option specified explicitly in the call to count().

Attachments

eager_count_performed_on_a_has_many_association_with_multi_table_conditional.diff (1.6 kB) - added by danger on 08/03/07 07:18:25.
eager_count_performed_on_a_has_many_association_with_multi_table_conditional_stable.diff (1.6 kB) - added by danger on 08/15/07 14:04:04.
Patch for the 1-2 stable branch

Change History

08/03/07 07:18:25 changed by danger

  • attachment eager_count_performed_on_a_has_many_association_with_multi_table_conditional.diff added.

08/03/07 07:41:05 changed by danger

Note: this is not the same problem as #9167. That one baffles me, this one's a two-character fix.

08/03/07 09:41:02 changed by lifofifo

I don't understand the significance of :include in a count query. Can you please elaborate ?

Thanks.

08/03/07 20:57:49 changed by danger

In the examples I gave in the ticket description I'm trying to count the number of posts that have no comments. The way to do that is to include the comments table with a join and test for "comments.id is null".

My real-world use case is that I'm attempting to detect products that have an associated object that's several tables away. My (totally renamed) code is:

class Customer < ActiveRecord::Base
  has_many :products
  has_many :unpurchased_products, :class_name => 'Product', :conditions => "line_items.it is not null OR estimates.id is null", :include => {:line_items => {:stores => :estimates} }
end

calling @customer.unpurchased_products works perfectly. But when I want to just display a number of unpurchased products in a shopping cart link at the top of the page I don't want to load all the data as objects. I just want to count it, which is why It's disconcerting that @customer.unpurchased_products.size doesn't work.

So this is a micro-patch that completely solves this behavior by making sure ActiveRecord doesn't clobber the specified :include tables to be joined.

08/07/07 23:10:28 changed by danger

  • keywords set to tiny patch.

08/15/07 14:04:04 changed by danger

  • attachment eager_count_performed_on_a_has_many_association_with_multi_table_conditional_stable.diff added.

Patch for the 1-2 stable branch

08/15/07 14:08:16 changed by danger

  • keywords changed from tiny patch to tiny patch needs_review.

08/16/07 04:37:34 changed by nzkoz

  • status changed from new to closed.
  • resolution set to fixed.

(In [7326]) Don't clobber :includes passed to count on has_many association. Closes #9175 [danger]

08/16/07 04:58:48 changed by nzkoz

(In [7328]) Don't clobber :includes passed to count on has_many association. Closes #9175 [danger]