mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #36284 from kamipo/fix_eager_loading_with_string_joins
Fix eager loading associations with string joins not to raise NoMethodError
This commit is contained in:
commit
13d6aa3a7b
3 changed files with 23 additions and 2 deletions
|
@ -44,8 +44,7 @@ module ActiveRecord
|
|||
|
||||
unless others.empty?
|
||||
joins.concat arel.join_sources
|
||||
right = joins.last.right
|
||||
right.expr.children.concat(others)
|
||||
append_constraints(joins.last, others)
|
||||
end
|
||||
|
||||
# The current table in this iteration becomes the foreign table in the next
|
||||
|
@ -65,6 +64,16 @@ module ActiveRecord
|
|||
|
||||
@readonly = reflection.scope && reflection.scope_for(base_klass.unscoped).readonly_value
|
||||
end
|
||||
|
||||
private
|
||||
def append_constraints(join, constraints)
|
||||
if join.is_a?(Arel::Nodes::StringJoin)
|
||||
join_string = table.create_and(constraints.unshift(join.left))
|
||||
join.left = Arel.sql(base_klass.connection.visitor.compile(join_string))
|
||||
else
|
||||
join.right.expr.children.concat(constraints)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -101,6 +101,17 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|||
assert_equal [taggings(:normal_comment_rating)], rating.taggings_without_tag
|
||||
end
|
||||
|
||||
def test_loading_association_with_string_joins
|
||||
rating = Rating.first
|
||||
assert_equal [taggings(:normal_comment_rating)], rating.taggings_with_no_tag
|
||||
|
||||
rating = Rating.preload(:taggings_with_no_tag).first
|
||||
assert_equal [taggings(:normal_comment_rating)], rating.taggings_with_no_tag
|
||||
|
||||
rating = Rating.eager_load(:taggings_with_no_tag).first
|
||||
assert_equal [taggings(:normal_comment_rating)], rating.taggings_with_no_tag
|
||||
end
|
||||
|
||||
def test_loading_with_scope_including_joins
|
||||
member = Member.first
|
||||
assert_equal members(:groucho), member
|
||||
|
|
|
@ -4,4 +4,5 @@ class Rating < ActiveRecord::Base
|
|||
belongs_to :comment
|
||||
has_many :taggings, as: :taggable
|
||||
has_many :taggings_without_tag, -> { left_joins(:tag).where("tags.id": nil) }, as: :taggable, class_name: "Tagging"
|
||||
has_many :taggings_with_no_tag, -> { joins("LEFT OUTER JOIN tags ON tags.id = taggings.tag_id").where("tags.id": nil) }, as: :taggable, class_name: "Tagging"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue