From 5c89d1689981a31bb7e54f0d23269786d222183c Mon Sep 17 00:00:00 2001 From: Michael Smedberg Date: Fri, 26 Jan 2018 14:17:32 -0800 Subject: [PATCH] :inherit associations should properly scope :through joins --- lib/active_record_inherit_assoc.rb | 7 ++++++- test/schema.rb | 13 +++++++++++++ test/test_inherit_assoc.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/active_record_inherit_assoc.rb b/lib/active_record_inherit_assoc.rb index 31c060c..38608bd 100644 --- a/lib/active_record_inherit_assoc.rb +++ b/lib/active_record_inherit_assoc.rb @@ -31,7 +31,12 @@ def target_scope def attribute_inheritance_hash return nil unless reflection.options[:inherit] - Array(reflection.options[:inherit]).inject({}) { |hash, association| hash[association] = owner.send(association) ; hash } + Array(reflection.options[:inherit]).inject({}) do |hash, association| + assoc_value = owner.send(association) + hash[association] = assoc_value + hash["#{through_reflection.table_name}.#{association}"] = assoc_value if reflection.options.key?(:through) + hash + end end if ActiveRecord::VERSION::MAJOR >= 4 diff --git a/test/schema.rb b/test/schema.rb index 45cf0aa..2eec1e3 100644 --- a/test/schema.rb +++ b/test/schema.rb @@ -25,4 +25,17 @@ t.integer :account_id t.integer :blah_id end + + drop_table(:fifths) rescue nil + create_table "fifths" do |t| + t.integer :main_id + t.integer :account_id + t.integer :sixth_id + end + + drop_table(:sixths) rescue nil + create_table "sixths" do |t| + t.integer :main_id + t.integer :account_id + end end diff --git a/test/test_inherit_assoc.rb b/test/test_inherit_assoc.rb index 770c18b..4db511f 100644 --- a/test/test_inherit_assoc.rb +++ b/test/test_inherit_assoc.rb @@ -8,6 +8,8 @@ class Main < ActiveRecord::Base if ActiveRecord::VERSION::MAJOR < 4 has_many :conditional_others, :inherit => :account_id, :conditions => {:val => "foo"}, :class_name => "Other" end + has_many :fifths, :inherit => :account_id + has_many :sixths, :through => :fifths, inherit: :account_id end class Other < ActiveRecord::Base @@ -22,6 +24,15 @@ class Fourth < ActiveRecord::Base belongs_to :main end + class Fifth < ActiveRecord::Base + belongs_to :main, inherit: :account_id + belongs_to :sixth, inherit: :account_id + end + + class Sixth < ActiveRecord::Base + belongs_to :main + end + describe "Main, with some others, scoped by account_id" do before do @main = Main.create! :account_id => 1 @@ -96,6 +107,25 @@ def test_has_one_should_set_conditions_on_includes_with_multiple_owners assert_equal third_4, mains.last.third end + def test_has_many_through_should_set_conditions_on_join_table + main_1 = Main.create! :account_id => 1 + main_2 = Main.create! :account_id => 1 + + sixth_1 = Sixth.create! :main_id => main_1.id, :account_id => 1 + sixth_2 = Sixth.create! :main_id => main_1.id, :account_id => 1 + sixth_3 = Sixth.create! :main_id => main_1.id, :account_id => 1 + sixth_4 = Sixth.create! :main_id => main_1.id, :account_id => 1 + + Fifth.create! :main_id => main_1.id, :account_id => 2, :sixth_id => sixth_1.id + Fifth.create! :main_id => main_1.id, :account_id => 1, :sixth_id => sixth_2.id + Fifth.create! :main_id => main_2.id, :account_id => 2, :sixth_id => sixth_3.id + Fifth.create! :main_id => main_2.id, :account_id => 1, :sixth_id => sixth_4.id + + mains = Main.where(id: [main_1.id, main_2.id]) + assert_equal [sixth_2], mains.first.sixths + assert_equal [sixth_4], mains.last.sixths + end + def test_has_many_should_set_conditions_on_includes main = Main.create! :account_id => 1, :blah_id => 10