Skip to content

Commit

Permalink
Obey validate: false option for habtm
Browse files Browse the repository at this point in the history
Fixes #14383.
  • Loading branch information
carlosantoniodasilva committed Mar 14, 2014
1 parent 2e87984 commit 1ca204b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -1584,7 +1584,7 @@ def destroy_associations
hm_options[:through] = middle_reflection.name hm_options[:through] = middle_reflection.name
hm_options[:source] = join_model.right_reflection.name hm_options[:source] = join_model.right_reflection.name


[:before_add, :after_add, :before_remove, :after_remove, :autosave].each do |k| [:before_add, :after_add, :before_remove, :after_remove, :autosave, :validate].each do |k|
hm_options[k] = options[k] if options.key? k hm_options[k] = options[k] if options.key? k
end end


Expand Down
Expand Up @@ -11,6 +11,7 @@
require 'models/tag' require 'models/tag'
require 'models/tagging' require 'models/tagging'
require 'models/parrot' require 'models/parrot'
require 'models/person'
require 'models/pirate' require 'models/pirate'
require 'models/treasure' require 'models/treasure'
require 'models/price_estimate' require 'models/price_estimate'
Expand Down Expand Up @@ -795,4 +796,27 @@ def test_destruction_does_not_error_without_primary_key
end end
end end


def test_association_with_validate_false_does_not_run_associated_validation_callbacks_on_create
rich_person = RichPerson.new

treasure = Treasure.new
treasure.rich_people << rich_person
treasure.valid?

assert_equal 1, treasure.rich_people.size
assert_nil rich_person.first_name, 'should not run associated person validation on create when validate: false'
end

def test_association_with_validate_false_does_not_run_associated_validation_callbacks_on_update
rich_person = RichPerson.create!
person_first_name = rich_person.first_name
assert_not_nil person_first_name

treasure = Treasure.new
treasure.rich_people << rich_person
treasure.valid?

assert_equal 1, treasure.rich_people.size
assert_equal person_first_name, rich_person.first_name, 'should not run associated person validation on update when validate: false'
end
end end
13 changes: 13 additions & 0 deletions activerecord/test/models/person.rb
Expand Up @@ -89,6 +89,19 @@ class RichPerson < ActiveRecord::Base
self.table_name = 'people' self.table_name = 'people'


has_and_belongs_to_many :treasures, :join_table => 'peoples_treasures' has_and_belongs_to_many :treasures, :join_table => 'peoples_treasures'

before_validation :run_before_create, on: :create
before_validation :run_before_validation

private

def run_before_create
self.first_name = first_name.to_s + 'run_before_create'
end

def run_before_validation
self.first_name = first_name.to_s + 'run_before_validation'
end
end end


class NestedPerson < ActiveRecord::Base class NestedPerson < ActiveRecord::Base
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/treasure.rb
Expand Up @@ -3,6 +3,7 @@ class Treasure < ActiveRecord::Base
belongs_to :looter, :polymorphic => true belongs_to :looter, :polymorphic => true


has_many :price_estimates, :as => :estimate_of has_many :price_estimates, :as => :estimate_of
has_and_belongs_to_many :rich_people, join_table: 'peoples_treasures', validate: false


accepts_nested_attributes_for :looter accepts_nested_attributes_for :looter
end end
Expand Down

0 comments on commit 1ca204b

Please sign in to comment.