Skip to content

Commit 0a48cce

Browse files
committed
Merge pull request rails#11496 from jetthoughts/11376_has_many_assoc_respect_scope_on_build
Removed where_values_hash from AR::NullRelation Conflicts: activerecord/CHANGELOG.md
1 parent 8176006 commit 0a48cce

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## unreleased ##
22

3+
* Objects intiantiated using a null relationship will now retain the
4+
attributes of the where clause.
5+
6+
Fixes: #11676, #11675, #11376
7+
8+
*Paul Nikitochkin*, *Peter Brown*, *Nthalk*
9+
310
* Fixed `ActiveRecord::Associations::CollectionAssociation#find`
411
when using `has_many` association with `:inverse_of` and finding an array of one element,
512
it should return an array of one element too.

activerecord/lib/active_record/null_relation.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ def to_sql
4242
@to_sql ||= ""
4343
end
4444

45-
def where_values_hash
46-
{}
47-
end
48-
4945
def count(*)
5046
0
5147
end

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ def test_create_from_association_should_respect_default_scope
135135
assert_equal 'exotic', bulb.name
136136
end
137137

138+
def test_build_from_association_should_respect_scope
139+
author = Author.new
140+
141+
post = author.thinking_posts.build
142+
assert_equal 'So I was thinking', post.title
143+
end
144+
138145
def test_create_from_association_with_nil_values_should_work
139146
car = Car.create(:name => 'honda')
140147

@@ -1702,6 +1709,22 @@ def test_association_attributes_are_available_to_after_initialize
17021709
assert_equal car.id, bulb.attributes_after_initialize['car_id']
17031710
end
17041711

1712+
def test_attributes_are_set_when_initialized_from_has_many_null_relationship
1713+
car = Car.new name: 'honda'
1714+
bulb = car.bulbs.where(name: 'headlight').first_or_initialize
1715+
assert_equal 'headlight', bulb.name
1716+
end
1717+
1718+
def test_attributes_are_set_when_initialized_from_polymorphic_has_many_null_relationship
1719+
post = Post.new title: 'title', body: 'bar'
1720+
tag = Tag.create!(name: 'foo')
1721+
1722+
tagging = post.taggings.where(tag: tag).first_or_initialize
1723+
1724+
assert_equal tag.id, tagging.tag_id
1725+
assert_equal 'Post', tagging.taggable_type
1726+
end
1727+
17051728
def test_replace
17061729
car = Car.create(:name => 'honda')
17071730
bulb1 = car.bulbs.create

activerecord/test/cases/relations_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ def test_null_relation_metadata_methods
295295
assert_equal({}, Developer.none.where_values_hash)
296296
end
297297

298+
def test_null_relation_where_values_hash
299+
assert_equal({ 'salary' => 100_000 }, Developer.none.where(salary: 100_000).where_values_hash)
300+
end
301+
298302
def test_joins_with_nil_argument
299303
assert_nothing_raised { DependentFirm.joins(nil).first }
300304
end

0 commit comments

Comments
 (0)