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
class ModelB < ActiveRecord::Base
  belongs_to :model_a
  validates_presence_of :name

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 << :name => nil
  a.valid? # => false

However, if I do something like this:

  a = ModelA.create
  a.model_bs << ModelB.create
  a.valid? # => true = 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 »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Shared Ticket Bins