Skip to content

Commit

Permalink
Merge pull request #13978 from Fortisque/kevin/validation_context_for…
Browse files Browse the repository at this point in the history
…_children

context in validation goes through has many relationship
  • Loading branch information
tenderlove committed Feb 9, 2014
2 parents 3b684dd + 5e3d466 commit f99b254
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -301,7 +301,7 @@ def validate_collection_association(reflection)
def association_valid?(reflection, record) def association_valid?(reflection, record)
return true if record.destroyed? || record.marked_for_destruction? return true if record.destroyed? || record.marked_for_destruction?


unless valid = record.valid? unless valid = record.valid?(self.validation_context)
if reflection.options[:autosave] if reflection.options[:autosave]
record.errors.each do |attribute, message| record.errors.each do |attribute, message|
attribute = "#{reflection.name}.#{attribute}" attribute = "#{reflection.name}.#{attribute}"
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -22,6 +22,8 @@
require 'models/categorization' require 'models/categorization'
require 'models/minivan' require 'models/minivan'
require 'models/speedometer' require 'models/speedometer'
require 'models/pirate'
require 'models/ship'


class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
fixtures :authors, :posts, :comments fixtures :authors, :posts, :comments
Expand Down Expand Up @@ -1830,4 +1832,12 @@ def test_collection_association_with_private_kernel_method
end end
end end
end end

test 'has_many_association passes context validation to validate children' do
pirate = FamousPirate.new
pirate.famous_ships << ship = FamousShip.new
assert_equal true, pirate.valid?
assert_equal false, pirate.valid?(:conference)
assert_equal "can't be blank", ship.errors[:name].first
end
end end
8 changes: 8 additions & 0 deletions activerecord/test/models/pirate.rb
Expand Up @@ -85,3 +85,11 @@ def log(record, callback)
class DestructivePirate < Pirate class DestructivePirate < Pirate
has_one :dependent_ship, :class_name => 'Ship', :foreign_key => :pirate_id, :dependent => :destroy has_one :dependent_ship, :class_name => 'Ship', :foreign_key => :pirate_id, :dependent => :destroy
end end

class FamousPirate < ActiveRecord::Base
self.table_name = 'pirates'

has_many :famous_ships

validates_presence_of :catchphrase, on: :conference
end
8 changes: 8 additions & 0 deletions activerecord/test/models/ship.rb
Expand Up @@ -17,3 +17,11 @@ def cancel_save_callback_method
false false
end end
end end

class FamousShip < ActiveRecord::Base
self.table_name = 'ships'

belongs_to :famous_pirate

validates_presence_of :name, on: :conference
end

0 comments on commit f99b254

Please sign in to comment.