Skip to content

Commit 73f2d37

Browse files
Wolfram Arnoldjosevalim
authored andcommitted
Add test to verify that the new :inverse_of association option will indeed fix the validation problem for a belongs_to relationship that validates_presence_of the parent, when both the parent and the child are new (in-memory) records. Also check that this works when the parents adds child via nested_attributes_for.
Lastly, add a require 'models/pet' to association_validation_test.rb, so that test can be run independently (was failing due to that missing dependency). [#2815 status:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
1 parent fbe6c3c commit 73f2d37

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

activerecord/test/cases/nested_attributes_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require "models/bird"
55
require "models/parrot"
66
require "models/treasure"
7+
require "models/man"
8+
require "models/interest"
79
require 'active_support/hash_with_indifferent_access'
810

911
module AssertRaiseWithMessage
@@ -470,6 +472,41 @@ def test_should_automatically_enable_autosave_on_the_association
470472
assert Pirate.reflect_on_association(@association_name).options[:autosave]
471473
end
472474

475+
def test_validate_presence_of_parent__works_with_inverse_of
476+
Man.accepts_nested_attributes_for(:interests)
477+
assert_equal :man, Man.reflect_on_association(:interests).options[:inverse_of]
478+
assert_equal :interests, Interest.reflect_on_association(:man).options[:inverse_of]
479+
480+
repair_validations(Interest) do
481+
Interest.validates_presence_of(:man)
482+
assert_difference 'Man.count' do
483+
assert_difference 'Interest.count', 2 do
484+
man = Man.create!(:name => 'John',
485+
:interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
486+
assert_equal 2, man.interests.count
487+
end
488+
end
489+
end
490+
end
491+
492+
def test_validate_presence_of_parent__fails_without_inverse_of
493+
Man.accepts_nested_attributes_for(:interests)
494+
Man.reflect_on_association(:interests).options.delete(:inverse_of)
495+
Interest.reflect_on_association(:man).options.delete(:inverse_of)
496+
497+
repair_validations(Interest) do
498+
Interest.validates_presence_of(:man)
499+
assert_no_difference ['Man.count', 'Interest.count'] do
500+
man = Man.create(:name => 'John',
501+
:interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
502+
assert !man.errors[:interests_man].empty?
503+
end
504+
end
505+
# restore :inverse_of
506+
Man.reflect_on_association(:interests).options[:inverse_of] = :man
507+
Interest.reflect_on_association(:man).options[:inverse_of] = :interests
508+
end
509+
473510
private
474511

475512
def association_setter

activerecord/test/cases/validations/association_validation_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
require 'models/topic'
44
require 'models/reply'
55
require 'models/owner'
6+
require 'models/pet'
7+
require 'models/man'
8+
require 'models/interest'
69

710
class AssociationValidationTest < ActiveRecord::TestCase
811
fixtures :topics, :owners
@@ -98,4 +101,24 @@ def test_validates_size_of_association_utf8
98101
end
99102
end
100103
end
104+
105+
def test_validates_presence_of_belongs_to_association__parent_is_new_record
106+
repair_validations(Interest) do
107+
# Note that Interest and Man have the :inverse_of option set
108+
Interest.validates_presence_of(:man)
109+
man = Man.new(:name => 'John')
110+
interest = man.interests.build(:topic => 'Airplanes')
111+
assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a man object associated"
112+
end
113+
end
114+
115+
def test_validates_presence_of_belongs_to_association__existing_parent
116+
repair_validations(Interest) do
117+
Interest.validates_presence_of(:man)
118+
man = Man.create!(:name => 'John')
119+
interest = man.interests.build(:topic => 'Airplanes')
120+
assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a man object associated"
121+
end
122+
end
123+
101124
end

0 commit comments

Comments
 (0)