Skip to content

Commit

Permalink
use name specified by 'as' for automatic inverse association to avoid…
Browse files Browse the repository at this point in the history
… reflecting on wrong association
  • Loading branch information
ashanbrown committed Jun 11, 2014
1 parent d075c84 commit 9feadc1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def inverse_name
# returns either nil or the inverse association name that it finds.
def automatic_inverse_of
if can_find_inverse_of_automatically?(self)
inverse_name = ActiveSupport::Inflector.underscore(active_record.name).to_sym
inverse_name = ActiveSupport::Inflector.underscore(options[:as] || active_record.name).to_sym

begin
reflection = klass.reflect_on_association(inverse_name)
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/associations/inverse_associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ def test_polymorphic_and_has_many_through_relationships_should_not_have_inverses
assert_respond_to club_reflection, :has_inverse?
assert !club_reflection.has_inverse?, "A has_many_through association should not find an inverse automatically"
end

def test_polymorphic_relationships_should_still_not_have_inverses_when_non_polymorphic_relationship_has_the_same_name
man_reflection = Man.reflect_on_association(:polymorphic_face_without_inverse)
face_reflection = Face.reflect_on_association(:man)

assert_respond_to face_reflection, :has_inverse?
assert face_reflection.has_inverse?, "For this test, the non-polymorphic association must have an inverse"

assert_respond_to man_reflection, :has_inverse?
assert !man_reflection.has_inverse?, "The target of a polymorphic association should not find an inverse automatically"
end
end

class InverseAssociationTests < ActiveRecord::TestCase
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/face.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Face < ActiveRecord::Base
belongs_to :man, :inverse_of => :face
belongs_to :polymorphic_man, :polymorphic => true, :inverse_of => :polymorphic_face
belongs_to :polymorphic_man_without_inverse, :polymorphic => true
# These is a "broken" inverse_of for the purposes of testing
belongs_to :horrible_man, :class_name => 'Man', :inverse_of => :horrible_face
belongs_to :horrible_polymorphic_man, :polymorphic => true, :inverse_of => :horrible_polymorphic_face
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/man.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Man < ActiveRecord::Base
has_one :face, :inverse_of => :man
has_one :polymorphic_face, :class_name => 'Face', :as => :polymorphic_man, :inverse_of => :polymorphic_man
has_one :polymorphic_face_without_inverse, :class_name => 'Face', :as => :polymorphic_man_without_inverse
has_many :interests, :inverse_of => :man
has_many :polymorphic_interests, :class_name => 'Interest', :as => :polymorphic_man, :inverse_of => :polymorphic_man
# These are "broken" inverse_of associations for the purposes of testing
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ def except(adapter_names_to_exclude)
t.integer :man_id
t.integer :polymorphic_man_id
t.string :polymorphic_man_type
t.integer :polymorphic_man_without_inverse_id
t.string :polymorphic_man_without_inverse_type
t.integer :horrible_polymorphic_man_id
t.string :horrible_polymorphic_man_type
end
Expand Down

0 comments on commit 9feadc1

Please sign in to comment.