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.

AR callback not invoked on has_one child when saved through parent

#2483

See the attached test or the code below. This test passes in Rails 2.2.2 and fails in Rails 2.3.2.

require File.dirname(__FILE__) + '/../test_helper'

ActiveRecord::Schema.define do
  create_table :parents, :force => true do |t|
  end

  create_table :children, :force => true do |t|
    t.integer :parent_id
  end
end

class InBeforeValidation < StandardError; end

class Parent < ActiveRecord::Base
  has_one :child

  before_create do |model|
    model.build_child
  end
end

class Child < ActiveRecord::Base
  belongs_to :parent

  before_validation do |model|
    raise InBeforeValidation
  end
end

class HasOneCallbackTest < ActiveSupport::TestCase
  test "The child's before_validation callback is invoked when created and saved by the parent" do
    assert_raise InBeforeValidation do
      Parent.create
    end
  end
end

Reported by Myron Marston · April 11th, 2009 @ 10:10 AM

State: duplicate
Milestone: 2.x
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. Myron Marston
    Myron Marston

    After searching through lighthouse some more, I can see that this is essentially the same issue, just with different effects. The patch in that ticket fixes this.

    April 11th, 2009 @ 11:11 PM

  2. Myron Marston
  3. David Trasbo
    David Trasbo
    • Assigned user set to Ryan Bigg

    This can be marked as a duplicate.

    April 14th, 2010 @ 08:49 PM

  4. Ryan Bigg