diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-05-08 17:39:02 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-05-09 22:26:55 +0200 |
commit | f4511690994f0c14c60320bbeecc66549332dbee (patch) | |
tree | d96262e1fe5de95b10b472929f916b2bf784c074 | |
parent | 28232ce99cea0d006de7b135efad2674e3f65c38 (diff) | |
download | rails-f4511690994f0c14c60320bbeecc66549332dbee.tar.gz rails-f4511690994f0c14c60320bbeecc66549332dbee.tar.bz2 rails-f4511690994f0c14c60320bbeecc66549332dbee.zip |
test, regression test for has_many with instance dependent scope.
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/models/author.rb | 2 |
2 files changed, 12 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 2453d6cf58..5f01352ab4 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1894,4 +1894,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_not pirate.valid?(:conference) assert_equal "can't be blank", ship.errors[:name].first end + + test 'association with instance dependent scope' do + bob = authors(:bob) + Post.create!(title: "signed post by bob", body: "stuff", author: authors(:bob)) + Post.create!(title: "anonymous post", body: "more stuff", author: authors(:bob)) + assert_equal ["misc post by bob", "other post by bob", + "signed post by bob"], bob.posts_with_signature.map(&:title).sort + + assert_equal [], authors(:david).posts_with_signature.map(&:title) + end end diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index c197951c71..f2463de522 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -140,6 +140,8 @@ class Author < ActiveRecord::Base has_many :posts_with_default_include, :class_name => 'PostWithDefaultInclude' has_many :comments_on_posts_with_default_include, :through => :posts_with_default_include, :source => :comments + has_many :posts_with_signature, -> (record) { where("posts.title LIKE ?", "%by #{record.name.downcase}%") }, class_name: "Post" + scope :relation_include_posts, -> { includes(:posts) } scope :relation_include_tags, -> { includes(:tags) } |