Skip to content

Commit

Permalink
Ensure association preloading properly merges default scope and assoc…
Browse files Browse the repository at this point in the history
…iation conditions
  • Loading branch information
lifo committed Aug 28, 2012
1 parent e6e9e56 commit 2d6d8a7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
Expand Up @@ -117,9 +117,7 @@ def process_conditions(conditions)
conditions = klass.send(:instance_eval, &conditions) conditions = klass.send(:instance_eval, &conditions)
end end


if conditions conditions
klass.send(:sanitize_sql, conditions)
end
end end
end end
end end
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -975,6 +975,18 @@ def test_preload_has_many_using_primary_key
end end
end end


def test_preload_has_many_with_association_condition_and_default_scope
post = Post.create!(:title => 'Beaches', :body => "I like beaches!")
Reader.create! :person => people(:david), :post => post
LazyReader.create! :person => people(:susan), :post => post

assert_equal 1, post.lazy_readers.to_a.size
assert_equal 2, post.lazy_readers_skimmers_or_not.to_a.size

post_with_readers = Post.includes(:lazy_readers_skimmers_or_not).find(post.id)
assert_equal 2, post_with_readers.lazy_readers_skimmers_or_not.to_a.size
end

def test_include_has_many_using_primary_key def test_include_has_many_using_primary_key
expected = Firm.find(1).clients_using_primary_key.sort_by(&:name) expected = Firm.find(1).clients_using_primary_key.sort_by(&:name)
# Oracle adapter truncates alias to 30 characters # Oracle adapter truncates alias to 30 characters
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -108,6 +108,7 @@ def add_joins_and_select


has_many :readers has_many :readers
has_many :readers_with_person, :include => :person, :class_name => "Reader" has_many :readers_with_person, :include => :person, :class_name => "Reader"

has_many :people, :through => :readers has_many :people, :through => :readers
has_many :single_people, :through => :readers has_many :single_people, :through => :readers
has_many :people_with_callbacks, :source=>:person, :through => :readers, has_many :people_with_callbacks, :source=>:person, :through => :readers,
Expand All @@ -118,6 +119,9 @@ def add_joins_and_select
has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true } has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true }
has_many :impatient_people, :through => :skimmers, :source => :person has_many :impatient_people, :through => :skimmers, :source => :person


has_many :lazy_readers
has_many :lazy_readers_skimmers_or_not, :conditions => { :skimmer => [ true, false ] }, :class_name => 'LazyReader'

def self.top(limit) def self.top(limit)
ranked_by_comments.limit_by(limit) ranked_by_comments.limit_by(limit)
end end
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/models/reader.rb
Expand Up @@ -3,3 +3,11 @@ class Reader < ActiveRecord::Base
belongs_to :person, :inverse_of => :readers belongs_to :person, :inverse_of => :readers
belongs_to :single_person, :class_name => 'Person', :foreign_key => :person_id, :inverse_of => :reader belongs_to :single_person, :class_name => 'Person', :foreign_key => :person_id, :inverse_of => :reader
end end

class LazyReader < ActiveRecord::Base
set_table_name 'readers'
default_scope where(:skimmer => true)

belongs_to :post
belongs_to :person
end

0 comments on commit 2d6d8a7

Please sign in to comment.