Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8241: remove_warning_in_validates_presence_of_docs.patch

File remove_warning_in_validates_presence_of_docs.patch, 2.9 kB (added by quixoten, 2 years ago)
  • lib/active_record/validations.rb

    old new  
    448448      # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 
    449449      # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    450450      # method, proc or string should return or evaluate to a true or false value. 
    451       # 
    452       # === Warning 
    453       # Validate the presence of the foreign key, not the instance variable itself. 
    454       # Do this: 
    455       #  validates_presence_of :invoice_id 
    456       # 
    457       # Not this: 
    458       #  validates_presence_of :invoice 
    459       # 
    460       # If you validate the presence of the associated object, you will get 
    461       # failures on saves when both the parent object and the child object are 
    462       # new. 
    463451      def validates_presence_of(*attr_names) 
    464452        configuration = { :message => ActiveRecord::Errors.default_error_messages[:blank], :on => :save } 
    465453        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) 
  • test/validations_test.rb

    old new  
    22require 'fixtures/topic' 
    33require 'fixtures/reply' 
    44require 'fixtures/person' 
     5require 'fixtures/reader' 
    56require 'fixtures/developer' 
    67 
    78# The following methods in Topic are used in test_conditional_validation_* 
     
    2829    Topic.write_inheritable_attribute(:validate, nil) 
    2930    Topic.write_inheritable_attribute(:validate_on_create, nil) 
    3031    Topic.write_inheritable_attribute(:validate_on_update, nil) 
     32     
     33    Reader.write_inheritable_attribute(:validate, nil) 
     34    Person.write_inheritable_attribute(:validate, nil) 
    3135  end 
    3236 
    3337  def test_single_field_validation 
     
    276280 
    277281    assert t.save 
    278282  end 
     283   
     284  def test_validate_presence_of_belongs_to_association_with_unsaved_parent_and_child 
     285    Reader.validates_presence_of :person 
     286     
     287    child = Reader.new(:post_id => '1') 
     288    assert !child.save 
     289    assert_equal "can't be blank", child.errors.on(:person) 
     290     
     291    parent = child.person = Person.new() 
     292    assert child.new_record? 
     293    assert parent.new_record? 
     294    assert child.save 
     295  end 
     296   
     297  def test_validate_presence_of_has_many_association_with_unsaved_parent_and_child 
     298    Person.validates_presence_of :readers 
     299     
     300    parent = Person.new() 
     301    assert !parent.save 
     302    assert_equal "can't be blank", parent.errors.on(:readers) 
     303     
     304    child = parent.readers = [Reader.new(:post_id => '1')] 
     305    assert parent.new_record? 
     306    assert parent.readers[0].new_record? 
     307    assert parent.save 
     308  end 
    279309 
    280310  def test_validate_uniqueness 
    281311    Topic.validates_uniqueness_of(:title)