This project is archived and is in readonly mode.

#634 ✓stale
Brian Dainton

#create behaves like #create! on association proxies

Reported by Brian Dainton | July 26th, 2008 @ 09:09 PM | in 2.x

First, here's the branch of my Rails fork with the fix and tests exercising the (assumed correct) behavior for this issue.

In short, if you try to call #create on a HasAndBelongsToManyAssociation or HasManyThroughAssociation and pass in model attribute values that do not validate, the method no longer silently handles validation errors by producing an invalid AR instance and populating the object's 'errors'. Instead, it throws an ActiveRecord::RecordInvalid error (I expect this behavior from #create!, not #create).

Here are the updated tests which show the problem in action.

A validation added to the Person model:

class Person < ActiveRecord::Base
  has_many :posts, :through => :readers

  def validate 
    errors.add('first_name', 'is invalid') if read_attribute(:first_name) == 'InvalidName'
  end
end

Here are the updated tests for the HMT associations. The first test (a call to #create!) with an invalid :first_name properly raises the RecordInvalid error.

The second test (a call to #create) errors because it ALSO raises the RecordInvalid error.

class HasManyThroughAssociationsTest < ActiveRecord::TestCase
  fixtures :posts, :readers, :people

  def test_attempted_associate_with_invalid_create_exclamation_raises_error
    assert_raises(ActiveRecord::RecordInvalid) do
      posts(:thinking).people.create!(:first_name=>"InvalidName")
    end
  end
  
  def test_attempted_associate_with_invalid_create_does_not_raise_error
    person = posts(:thinking).people.create(:first_name=>"InvalidName")
    assert !person.valid?
    assert 1, person.errors.size
  end
end

Here are the updated tests for the HABTM associations. Likewise, the first test (a call to #create!) with an invalid :name properly raises the RecordInvalid error.

The second test (a call to #create) errors because it ALSO raises the RecordInvalid error.

class ProjectWithShortNamedDevelopers < ActiveRecord::Base
  set_table_name 'projects'
  has_and_belongs_to_many :developers,
    :class_name => "DeveloperWithShortName",
    :join_table => "developers_projects",
    :foreign_key => "project_id",
    :association_foreign_key => "developer_id"
end

class DeveloperWithShortName < ActiveRecord::Base
  set_table_name 'developers'
  has_and_belongs_to_many :projects,
    :class_name => "ProjectWithShortNamedDevelopers",
    :join_table => :developers_projects,
    :association_foreign_key => :project_id,
    :foreign_key => "developer_id"
    
  validates_length_of :name, :in => 3..10
end

class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
  def test_attempted_associate_with_invalid_create_exclamation_raises_error
    project = ProjectWithShortNamedDevelopers.find_by_name('Active Record')
    assert_raises(ActiveRecord::RecordInvalid) do
      project.developers.create!(:name => 'AnAbnormallyLongFirstName')
    end
  end
  
  def test_attempted_associate_with_invalid_create_does_not_raise_error
    project = ProjectWithShortNamedDevelopers.find_by_name('Active Record')
    developer = project.developers.create(:name => 'AnAbnormallyLongFirstName')
    assert !developer.valid?
    assert 1, developer.errors.size
  end
end

Tracking this down, it looks as if the #insert_record method on both of these associations is defaulting its 'force' param to true, when it should be false by default.

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages