aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-12-08 02:37:02 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-12-08 03:42:04 +0900
commit6a8ce7416d6615a13ee5c4b9f6bcd91cc5adef4d (patch)
treed6885c7eddc3232aa81dc72ebb12574b5bbe0b7c /activerecord/test
parentbbacd60048a8efa1777a01292a9392e146a7d885 (diff)
downloadrails-6a8ce7416d6615a13ee5c4b9f6bcd91cc5adef4d.tar.gz
rails-6a8ce7416d6615a13ee5c4b9f6bcd91cc5adef4d.tar.bz2
rails-6a8ce7416d6615a13ee5c4b9f6bcd91cc5adef4d.zip
Fix `scope_for_create` to do not lose polymorphic associations
This regression was caused at 213796fb due to polymorphic predicates are combined by `Arel::Nodes::And`. But I'd like to keep that combined because it would help inverting polymorphic predicates correctly (e9ba12f7), and we can collect equality nodes regardless of combined by `Arel::Nodes::And` (`a AND (b AND c) AND d` == `a AND b AND c AND d`). This change fixes the regression to collect equality nodes in `Arel::Nodes::And` as well. Fixes #31338.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/relation_test.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb9
2 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb
index a71d8de521..b424ca91de 100644
--- a/activerecord/test/cases/relation_test.rb
+++ b/activerecord/test/cases/relation_test.rb
@@ -68,7 +68,7 @@ module ActiveRecord
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
left = relation.table[:id].eq(10)
right = relation.table[:id].eq(10)
- combine = left.and right
+ combine = left.or(right)
relation.where! combine
assert_equal({}, relation.where_values_hash)
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 50ad1d5b26..675aafabda 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1189,6 +1189,15 @@ class RelationTest < ActiveRecord::TestCase
assert_equal "hen", hen.name
end
+ def test_create_with_polymorphic_association
+ author = authors(:david)
+ post = posts(:welcome)
+ comment = Comment.where(post: post, author: author).create!(body: "hello")
+
+ assert_equal author, comment.author
+ assert_equal post, comment.post
+ end
+
def test_first_or_create
parrot = Bird.where(color: "green").first_or_create(name: "parrot")
assert_kind_of Bird, parrot