mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make sure has_many associations honour :include when counting. Closes #9167 [danger]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7325 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
4954c77b69
commit
82b244e3ff
4 changed files with 11 additions and 1 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Make sure has_many uses :include when counting [danger]
|
||||
|
||||
* Change the implementation of ActiveRecord's attribute reader and writer methods [nzkoz]
|
||||
- Generate Reader and Writer methods which cache attribute values in hashes. This is to avoid repeatedly parsing the same date or integer columns.
|
||||
- Change exception raised when users use find with :select then try to access a skipped column. Plugins could override missing_attribute() to lazily load the columns.
|
||||
|
|
|
@ -99,7 +99,7 @@ module ActiveRecord
|
|||
elsif @reflection.options[:counter_sql]
|
||||
@reflection.klass.count_by_sql(@counter_sql)
|
||||
else
|
||||
@reflection.klass.count(:conditions => @counter_sql)
|
||||
@reflection.klass.count(:conditions => @counter_sql, :include => @reflection.options[:include])
|
||||
end
|
||||
|
||||
@target = [] and loaded if count == 0
|
||||
|
|
|
@ -272,6 +272,13 @@ class EagerAssociationTest < Test::Unit::TestCase
|
|||
assert_equal companies(:first_firm, :reload).account, f.account
|
||||
end
|
||||
|
||||
def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
|
||||
author = authors(:david)
|
||||
posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
|
||||
assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
|
||||
assert_equal posts_with_no_comments, author.posts_with_no_comments
|
||||
end
|
||||
|
||||
def test_eager_with_invalid_association_reference
|
||||
assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
|
||||
post = Post.find(6, :include=> :monkeys )
|
||||
|
|
1
activerecord/test/fixtures/author.rb
vendored
1
activerecord/test/fixtures/author.rb
vendored
|
@ -25,6 +25,7 @@ class Author < ActiveRecord::Base
|
|||
|
||||
has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'"
|
||||
has_many :hello_post_comments, :through => :hello_posts, :source => :comments
|
||||
has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments
|
||||
|
||||
has_many :other_posts, :class_name => "Post"
|
||||
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
|
||||
|
|
Loading…
Reference in a new issue