This project is archived and is in readonly mode.

#2483 ✓duplicate
Myron Marston

AR callback not invoked on has_one child when saved through parent

Reported by Myron Marston | April 11th, 2009 @ 10:10 AM | in 2.x

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

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

Attachments

Pages