Skip to content

Commit

Permalink
Merge pull request #17 from zendesk/smedberg/inherit_assoc_should_han…
Browse files Browse the repository at this point in the history
…dle_through

:inherit associations should properly scope :through joins
  • Loading branch information
Michael Smedberg authored Jan 26, 2018
2 parents f8d9851 + 5c89d16 commit 8de2293
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/active_record_inherit_assoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions test/test_inherit_assoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 8de2293

Please sign in to comment.