Navigation Menu

Skip to content

Commit

Permalink
Ensure empty has_many :through association preloaded via joins is mar…
Browse files Browse the repository at this point in the history
…ked as loaded. Fixes #2054.
  • Loading branch information
jonleighton committed Aug 15, 2011
1 parent 7a9da9c commit 6bb6519
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Expand Up @@ -188,13 +188,12 @@ def construct_association(record, join_part, row)
association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil? association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
set_target_and_inverse(join_part, association, record) set_target_and_inverse(join_part, association, record)
else else
return if row[join_part.aliased_primary_key].nil? association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
association = join_part.instantiate(row)
case macro case macro
when :has_many, :has_and_belongs_to_many when :has_many, :has_and_belongs_to_many
other = record.association(join_part.reflection.name) other = record.association(join_part.reflection.name)
other.loaded! other.loaded!
other.target.push(association) other.target.push(association) if association
other.set_inverse_instance(association) other.set_inverse_instance(association)
when :belongs_to when :belongs_to
set_target_and_inverse(join_part, association, record) set_target_and_inverse(join_part, association, record)
Expand Down
Expand Up @@ -808,4 +808,12 @@ def test_create_bang_returns_falsy_when_join_record_has_errors
assert !c.save assert !c.save
end end
end end

def test_preloading_empty_through_association_via_joins
person = Person.create!(:first_name => "Gaga")
person = Person.where(:id => person.id).where('readers.id = 1 or 1=1').includes(:posts).to_a.first

assert person.posts.loaded?, 'person.posts should be loaded'
assert_equal [], person.posts
end
end end

0 comments on commit 6bb6519

Please sign in to comment.