This project is archived and is in readonly mode.

#2149 ✓ invalid
Keith Pitt

Inconsistent has_many validation.

Reported by Keith Pitt | March 6th, 2009 @ 04:41 AM | in 2.x


class ModelA < ActiveRecord::Base
  has_many :model_bs
end
class ModelB < ActiveRecord::Base
  belongs_to :model_a
  validates_presence_of :name
end

I have 2 ActiveRecord model that look like the ones above. If I add an 'invalid' record to an existing record like this - the 'a' record becomes invalid.


  a = ModelA.create
  a.model_bs << ModelB.new :name => nil
  a.valid? # => false

However, if I do something like this:


  a = ModelA.create
  a.model_bs << ModelB.create
  a.valid? # => true
  a.model_bs.first.name = nil
  a.valid? # => true

the parent record is still valid even though I make an existing child record invalid. I have solved the problem by adding a:


  validates_associated :model_bs

but it seems a little odd to me to have an inconsistent default behaviour. Is this a bug or a feature?

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tickets have moved to Github

The new ticket tracker is available at https://github.com/rails/rails/issues

Shared Ticket Bins

Attachments

Pages