1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

context in validation goes through has many relationship

This commit is contained in:
Kevin Casey 2014-02-08 08:44:10 -08:00
parent ed66361488
commit 5e3d466d52
4 changed files with 27 additions and 1 deletions

View file

@ -301,7 +301,7 @@ module ActiveRecord
def association_valid?(reflection, record)
return true if record.destroyed? || record.marked_for_destruction?
unless valid = record.valid?
unless valid = record.valid?(self.validation_context)
if reflection.options[:autosave]
record.errors.each do |attribute, message|
attribute = "#{reflection.name}.#{attribute}"

View file

@ -22,6 +22,8 @@ require 'models/engine'
require 'models/categorization'
require 'models/minivan'
require 'models/speedometer'
require 'models/pirate'
require 'models/ship'
class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
fixtures :authors, :posts, :comments
@ -1830,4 +1832,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
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

View file

@ -85,3 +85,11 @@ end
class DestructivePirate < Pirate
has_one :dependent_ship, :class_name => 'Ship', :foreign_key => :pirate_id, :dependent => :destroy
end
class FamousPirate < ActiveRecord::Base
self.table_name = 'pirates'
has_many :famous_ships
validates_presence_of :catchphrase, on: :conference
end

View file

@ -17,3 +17,11 @@ class Ship < ActiveRecord::Base
false
end
end
class FamousShip < ActiveRecord::Base
self.table_name = 'ships'
belongs_to :famous_pirate
validates_presence_of :name, on: :conference
end