Permalink
Browse files

Merge pull request #16172 from Agis-/hmt_scope_arity

has_many :through with dynamic condition merging
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
1 parent 204ea85 commit 379ad762cfad6e6ef65af9da274fb3a97f5fc5b0 @rafaelfranca rafaelfranca committed Aug 20, 2014
@@ -1,3 +1,10 @@
+* Fix has_many :through relation merging failing when dynamic conditions are
+ passed as a lambda with an arity of one.
+
+ Fixes #16128.
+
+ *Agis Anastasopoulos*
+
* Fixed an issue where custom accessor methods (such as those generated by
`enum`) with the same name as a global method are incorrectly overridden
when subclassing.
@@ -15,7 +15,11 @@ def target_scope
scope = super
chain.drop(1).each do |reflection|
relation = reflection.klass.all
- relation.merge!(reflection.scope) if reflection.scope
+
+ reflection_scope = reflection.scope
+ if reflection_scope && reflection_scope.arity.zero?
+ relation.merge!(reflection_scope)
+ end
scope.merge!(
relation.except(:select, :create_with, :includes, :preload, :joins, :eager_load)
@@ -4,6 +4,7 @@
require 'models/developer'
require 'models/post'
require 'models/project'
+require 'models/rating'
class RelationMergingTest < ActiveRecord::TestCase
fixtures :developers, :comments, :authors, :posts
@@ -165,4 +166,16 @@ class MergingDifferentRelationsTest < ActiveRecord::TestCase
assert_equal ["Mary", "Mary", "Mary", "David"], posts_by_author_name
end
+
+ test "relation merging (using a proc argument)" do
+ dev = Developer.where(name: "Jamis").first
+
+ comment_1 = dev.comments.create!(body: "I'm Jamis", post: Post.first)
+ rating_1 = comment_1.ratings.create!
+
+ comment_2 = dev.comments.create!(body: "I'm John", post: Post.first)
+ rating_2 = comment_2.ratings.create!
+
+ assert_equal dev.ratings, [rating_1]
+ end
end
@@ -9,6 +9,7 @@ class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
belongs_to :author, polymorphic: true
belongs_to :resource, polymorphic: true
+ belongs_to :developer
has_many :ratings
@@ -46,6 +46,8 @@ def find_least_recent
has_many :audit_logs
has_many :contracts
has_many :firms, :through => :contracts, :source => :firm
+ has_many :comments, ->(developer) { where(body: "I'm #{developer.name}") }
+ has_many :ratings, through: :comments
scope :jamises, -> { where(:name => 'Jamis') }
@@ -195,6 +195,7 @@ def create_table(*args, &block)
t.references :author, polymorphic: true
t.string :resource_id
t.string :resource_type
+ t.integer :developer_id
end
create_table :companies, force: true do |t|

0 comments on commit 379ad76

Please sign in to comment.