This project is archived and is in readonly mode.
: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
-
George Ogata November 29th, 2009 @ 09:01 PM
- Tag set to activerecord, inverse_of, patch, tested
-
Eloy Duran December 17th, 2009 @ 11:15 AM
- Milestone set to 2.3.6
- State changed from new to verified
- Assigned user set to Michael Koziarski
Applies and runs.
-
Eloy Duran December 17th, 2009 @ 11:34 AM
Pushed this change to our repo: http://github.com/Fingertips/rails/commit/c433e9bd1ae5a6c4a32b456a5...
-
Murray Steele December 17th, 2009 @ 11:41 AM
- 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) -
Murray Steele December 17th, 2009 @ 11:41 AM
- Assigned user set to Michael Koziarski
Oops! Didn't mean to unassign this.
-
Repository December 28th, 2009 @ 08:29 PM
- 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... -
Repository December 28th, 2009 @ 08:29 PM
(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... -
Repository December 28th, 2009 @ 08:52 PM
(from [fc85c665271578e55e7fe90a721ca1533289d923]) 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/fc85c665271578e55e7fe90a721ca1... -
Repository December 28th, 2009 @ 08:52 PM
(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...
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
Referenced by
- 3533 Backport :inverse_of from 3.0 to 2.3.x Um... that 2nd link should be to https://rails.lighthous...
- 3520 Add inverse polymorphic association support a) includes George's patch tweaked to apply cleanly on to...
- 3533 Backport :inverse_of from 3.0 to 2.3.x Just reminding myself that when #3513 and/or #3520 are ap...
- 3513 :inverse_of not honored when replacing a has_one association (from [ed5b89483a1d9b0582e09d8e5fc5c38ecb0c809d]) Set inv...
- 3513 :inverse_of not honored when replacing a has_one association (from [592085be83ed1b8ff0065a92490e54fcb1a8bc87]) Add mor...
- 3513 :inverse_of not honored when replacing a has_one association (from [fc85c665271578e55e7fe90a721ca1533289d923]) Set inv...
- 3513 :inverse_of not honored when replacing a has_one association (from [6c8c85bc1eaf1639ea0df5f356e7105c74d128b2]) Add mor...