This project is archived and is in readonly mode.

#3513 ✓resolved
George Ogata

:inverse_of not honored when replacing a has_one association

Reported by George Ogata | November 26th, 2009 @ 05:12 AM | in 2.3.6

When assigning to, or calling #replace, on a has_one association, the inverse association is not set.

The patch attached fixes this.

require 'active_record'

ActiveRecord::Base.establish_connection('adapter' => "sqlite3", 'database' => ":memory:")
ActiveRecord::Schema.define do
  create_table :parents do
  end
  create_table :children do |t|
    t.integer :parent_id
  end
end

class Parent < ActiveRecord::Base
  has_one :child, :as => :parent, :inverse_of => :parent
end

class Child < ActiveRecord::Base
  belongs_to :parent, :inverse_of => :child
end

parent = Parent.new(:child => Child.new)
new_child = Child.new
parent.child = new_child

p parent.child.parent.target.equal?(parent)  # should be true

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>

Referenced by

Pages