Skip to content
Browse files

Merge pull request #11496 from jetthoughts/11376_has_many_assoc_respe…

…ct_scope_on_build

Removed where_values_hash from AR::NullRelation
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
1 parent 8176006 commit 0a48ccecde54fa867a01eb6ca0eab551990e33f6 @rafaelfranca rafaelfranca committed Sep 28, 2013
View
7 activerecord/CHANGELOG.md
@@ -1,5 +1,12 @@
## unreleased ##
+* Objects intiantiated using a null relationship will now retain the
+ attributes of the where clause.
+
+ Fixes: #11676, #11675, #11376
+
+ *Paul Nikitochkin*, *Peter Brown*, *Nthalk*
+
* Fixed `ActiveRecord::Associations::CollectionAssociation#find`
when using `has_many` association with `:inverse_of` and finding an array of one element,
it should return an array of one element too.
View
4 activerecord/lib/active_record/null_relation.rb
@@ -42,10 +42,6 @@ def to_sql
@to_sql ||= ""
end
- def where_values_hash
- {}
- end
-
def count(*)
0
end
View
23 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -135,6 +135,13 @@ def test_create_from_association_should_respect_default_scope
assert_equal 'exotic', bulb.name
end
+ def test_build_from_association_should_respect_scope
+ author = Author.new
+
+ post = author.thinking_posts.build
+ assert_equal 'So I was thinking', post.title
+ end
+
def test_create_from_association_with_nil_values_should_work
car = Car.create(:name => 'honda')
@@ -1702,6 +1709,22 @@ def test_association_attributes_are_available_to_after_initialize
assert_equal car.id, bulb.attributes_after_initialize['car_id']
end
+ def test_attributes_are_set_when_initialized_from_has_many_null_relationship
+ car = Car.new name: 'honda'
+ bulb = car.bulbs.where(name: 'headlight').first_or_initialize
+ assert_equal 'headlight', bulb.name
+ end
+
+ def test_attributes_are_set_when_initialized_from_polymorphic_has_many_null_relationship
+ post = Post.new title: 'title', body: 'bar'
+ tag = Tag.create!(name: 'foo')
+
+ tagging = post.taggings.where(tag: tag).first_or_initialize
+
+ assert_equal tag.id, tagging.tag_id
+ assert_equal 'Post', tagging.taggable_type
+ end
+
def test_replace
car = Car.create(:name => 'honda')
bulb1 = car.bulbs.create
View
4 activerecord/test/cases/relations_test.rb
@@ -295,6 +295,10 @@ def test_null_relation_metadata_methods
assert_equal({}, Developer.none.where_values_hash)
end
+ def test_null_relation_where_values_hash
+ assert_equal({ 'salary' => 100_000 }, Developer.none.where(salary: 100_000).where_values_hash)
+ end
+
def test_joins_with_nil_argument
assert_nothing_raised { DependentFirm.joins(nil).first }
end

0 comments on commit 0a48cce

Please sign in to comment.
Something went wrong with that request. Please try again.