Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

#create behaves like #create! on association proxies

#634

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.

Reported by Brian Dainton · July 26th, 2008 @ 09:09 PM

State: stale
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. josh
    josh
    • State changed from new to stale

    Staling out, please reopen if this is still a problem.

    October 28th, 2008 @ 04:34 PM