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.

:inverse_of not honored when replacing a has_one association

#3513

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

Reported by George Ogata · November 26th, 2009 @ 05:12 AM

State: resolved
Milestone: 2.3.6
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. George Ogata
    George Ogata
    • Tag set to activerecord, inverse_of, patch, tested

    November 29th, 2009 @ 09:01 PM

  2. Eloy Duran
    Eloy Duran
    • Milestone set to 2.3.6
    • State changed from new to verified
    • Assigned user set to Michael Koziarski

    Applies and runs.

    December 17th, 2009 @ 11:15 AM

  3. Eloy Duran
  4. Murray Steele
    Murray Steele
    • Assigned user cleared.

    I'm attaching a patch that should go on top of this one to:

    a) add more tests for the various ways you can assign things (bang methods, <<, =, replace) for all the association types
    b) get rid of a duplicate set_inverse_instance if you happen to use new_record(true)

    December 17th, 2009 @ 11:41 AM

  5. Murray Steele
    Murray Steele
    • Assigned user set to Michael Koziarski

    Oops! Didn't mean to unassign this.

    December 17th, 2009 @ 11:41 AM

  6. Repository
    Repository
    • State changed from verified to resolved

    (from [ed5b89483a1d9b0582e09d8e5fc5c38ecb0c809d]) Set inverse for #replace on a has_one association. [#3513 state:resolved]

    Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
    http://github.com/rails/rails/commit/ed5b89483a1d9b0582e09d8e5fc5c3...

    December 28th, 2009 @ 08:29 PM

  7. Repository
    Repository

    (from [592085be83ed1b8ff0065a92490e54fcb1a8bc87]) Add more tests for the various ways we can assign objects to associations. [#3513 state:resolved]

    Get rid of a duplicate set_inverse_instance call if you use new_record(true) (e.g. you want to replace the existing instance).

    Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
    http://github.com/rails/rails/commit/592085be83ed1b8ff0065a92490e54...

    December 28th, 2009 @ 08:29 PM

  8. Repository
  9. Repository
    Repository

    (from [6c8c85bc1eaf1639ea0df5f356e7105c74d128b2]) Add more tests for the various ways we can assign objects to associations. [#3513 state:resolved]

    Get rid of a duplicate set_inverse_instance call if you use new_record(true) (e.g. you want to replace the existing instance).

    Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
    http://github.com/rails/rails/commit/6c8c85bc1eaf1639ea0df5f356e710...

    December 28th, 2009 @ 08:52 PM