Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract join values to merge as an original type #16635

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
* Fix nesting merge `#joins' with String argument raises `ActiveRecord::ConfigurationError`.

Example:
```ruby
# Before
User.joins(:comments).merge(
Comment.joins(:article).merge(
Article.joins(
'LEFT OUTER JOIN categories ON articles.id = categories.article_id'
)
)
) # => ActiveRecord::ConfigurationError
```

*Takamichi Yoshikawa*

* Fix has_many :through relation merging failing when dynamic conditions are
passed as a lambda with an arity of one.

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def merge_joins
join_dependency = ActiveRecord::Associations::JoinDependency.new(other.klass,
joins_dependency,
[])
relation.joins! rest
relation.joins!(*rest)

@relation = relation.joins join_dependency
end
Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/relation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'models/comment'
require 'models/author'
require 'models/rating'
require 'models/tagging'

module ActiveRecord
class RelationTest < ActiveRecord::TestCase
Expand Down Expand Up @@ -236,6 +237,27 @@ def test_relation_merging_with_merged_joins_as_strings
assert_equal 3, authors(:david).posts.merge(posts_with_special_comments_with_ratings).count.length
end

def test_relation_merging_with_nested_joins_as_strings
post = Post.create!(title: "haha", body: "huhu")
comment = post.comments.create!(body: "hu")
comment.ratings.create!

join_string = <<-JOIN
LEFT OUTER JOIN
#{Tagging.quoted_table_name}
ON
#{Tagging.quoted_table_name}.taggable_id = #{Rating.quoted_table_name}.id AND #{Tagging.quoted_table_name}.taggable_type = 'Rating'
JOIN

relation = Post.where(id: post.id).joins(:comments).merge(
Comment.joins(:ratings).merge(
Rating.joins(join_string)
)
)

assert_equal 1, relation.count
end

class EnsureRoundTripTypeCasting < ActiveRecord::Type::Value
def type
:string
Expand Down