diff options
author | Paul Nikitochkin <paul.nikitochkin@gmail.com> | 2013-07-14 17:32:56 +0300 |
---|---|---|
committer | Paul Nikitochkin <paul.nikitochkin@gmail.com> | 2013-09-28 11:21:34 +0300 |
commit | 8fb0de2cae8e6f26c71ab8e4267d3841a38a29b9 (patch) | |
tree | 389517698e39008e1d8e817f2b27d71cd8107207 /activerecord/test/cases/associations | |
parent | 3e0a60e4e2316ee696bdcf1c115582f8f450ad07 (diff) | |
download | rails-8fb0de2cae8e6f26c71ab8e4267d3841a38a29b9.tar.gz rails-8fb0de2cae8e6f26c71ab8e4267d3841a38a29b9.tar.bz2 rails-8fb0de2cae8e6f26c71ab8e4267d3841a38a29b9.zip |
Removed where_values_hash from AR::NullRelation
In order to build associated records for owners which has not been saved
need to get where values to use as default attributes.
But for new record owner uses `ActiveRecord::NullRelation` which
override `where_values_hash` to return empty hash stub.
`where_values_hash` is not used to invoke any sql query, but good to
build others chains (even will be never executed) like:
```ruby
post = Post.new
admin_comment = post.admin_comments.build
assert_equal 'Admin', admin_comment.author
```
Closes #11376, #11676, #11675
Diffstat (limited to 'activerecord/test/cases/associations')
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 064e31f634..caa916346a 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -80,6 +80,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase 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') @@ -1629,6 +1636,22 @@ class HasManyAssociationsTest < ActiveRecord::TestCase 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 |