Skip to content

Commit

Permalink
Merge pull request #12664 from jetthoughts/12242_includes_in_through_…
Browse files Browse the repository at this point in the history
…association

Skip `include_values` from through associations chains for building association scope
Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/associations/association_scope.rb
  • Loading branch information
rafaelfranca committed Oct 27, 2013
1 parent bb22835 commit 9f03c61
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
24 changes: 24 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
## unreleased ##

* Do not add to scope includes values from through associations.
Fixed bug when providing `includes` in through association scope, and fetching targets.

Example:
class Vendor < ActiveRecord::Base
has_many :relationships, -> { includes(:user) }
has_many :users, through: :relationships
end

vendor = Vendor.first

# Before

vendor.users.to_a # => Raises exception: not found `:user` for `User`

# After

vendor.users.to_a # => No exception is raised


Fixes: #12242, #9517, #10240

*Paul Nikitochkin*

* Save `has_one` association when primary key is manually set.

Fixes #12302.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,22 @@ def add_constraints(scope)
scope = scope.joins(join(foreign_table, constraint))
end

is_first_chain = i == 0
klass = is_first_chain ? self.klass : reflection.klass

# Exclude the scope of the association itself, because that
# was already merged in the #scope method.
scope_chain[i].each do |scope_chain_item|
klass = i == 0 ? self.klass : reflection.klass
item = eval_scope(klass, scope_chain_item)

if scope_chain_item == self.reflection.scope
scope.merge! item.except(:where, :includes)
end

scope.includes! item.includes_values
if is_first_chain
scope.includes! item.includes_values
end

scope.where_values += item.where_values
scope.order_values |= item.order_values
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,4 +913,8 @@ def test_has_many_through_obeys_order_on_through_association
readers(:michael_authorless).update(first_post_id: 1)
assert_equal [posts(:thinking)], person.reload.first_posts
end

def test_has_many_through_with_includes_in_through_association_scope
posts(:welcome).author_address_extra_with_address.to_a
end
end
3 changes: 3 additions & 0 deletions activerecord/test/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def greeting
has_many :author_favorites, :through => :author
has_many :author_categorizations, :through => :author, :source => :categorizations
has_many :author_addresses, :through => :author
has_many :author_address_extra_with_address,
through: :author_with_address,
source: :author_address_extra

has_many :comments_with_interpolated_conditions,
->(p) { where "#{"#{p.aliased_table_name}." rescue ""}body = ?", 'Thank you for the welcome' },
Expand Down

0 comments on commit 9f03c61

Please sign in to comment.