This project is archived and is in readonly mode.
has_one through with new value nil
Reported by Daniel Guettler | July 24th, 2008 @ 08:06 PM | in 2.x
I ran into this when I tried to set a has_one through association to nil. ActiveRecord correctly removed the through association from the database but then it tried to create a new association using nil as associate.
class Member < ActiveRecord::Base
has_one :current_membership
has_one :club, :through => :current_membership
end
class CurrentMembership < Membership
belongs_to :member
belongs_to :club
end
class Club < ActiveRecord::Base
end
@member.club = nil
Attached a patch with failing test and possible fix.
Comments and changes to this ticket
-
josh October 28th, 2008 @ 04:32 PM
- State changed from new to stale
Staling out, please reopen if this is still a problem.
-
Daniel Guettler October 28th, 2008 @ 06:38 PM
Yes this is still a problem, I just tried it with the current edge version of rails and got:
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id from /Users/dguettler/workspace/my_projects/rails_testings/has_one_nil/vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:100:in `construct_join_attributes' from /Users/dguettler/workspace/my_projects/rails_testings/has_one_nil/vendor/rails/activerecord/lib/active_record/associations/has_one_through_association.rb:15:in `create_through_record' from /Users/dguettler/workspace/my_projects/rails_testings/has_one_nil/vendor/rails/activerecord/lib/active_record/associations.rb:1265:in `club=' from (irb):7
-
Daniel Guettler January 6th, 2009 @ 10:27 PM
It's still and issue in Rails 2.2.2 right now if you set @member.club = nil it actually doesn't complain about nil anymore but just takes the id of nil and updates the membership. So you end up with a club_id of 4. The old patch doesn't apply anymore to 2.2.2 I'll add a new one. Does anybody know how to "re-open" this ticket?
-
Tarmo Tänav January 7th, 2009 @ 07:09 AM
- State changed from stale to open
-
Daniel Guettler January 7th, 2009 @ 07:27 AM
Thanks for re-opening. I just went through the code and am not quite sure what should actually happen with the Membership when @member.club is set to nil. I basically see 2 options: 1. delete the entry 2. set the club_id to 0 Any opinions?
-
Daniel Guettler January 27th, 2009 @ 01:41 AM
ok, here an updated patch which applies to the 2.2 branch. It will not try to create a new entry if nil is passed and removes existing entries through entries if nil is passed
-
Repository May 17th, 2009 @ 01:54 PM
- State changed from open to resolved
(from [4cd40726eb10fbab269be53c27b304c728541dff]) has_one :through should not create a new association when assigned nil [#698 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/4cd40726eb10fbab269be53c27b304... -
Repository May 17th, 2009 @ 01:54 PM
(from [25724eede9b5a62c74b3b78245944638f1cfcef4]) has_one :through should not create a new association when assigned nil [#698 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/25724eede9b5a62c74b3b782459446...
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
Tags
Referenced by
- 698 has_one through with new value nil (from [4cd40726eb10fbab269be53c27b304c728541dff]) has_one...
- 698 has_one through with new value nil (from [25724eede9b5a62c74b3b78245944638f1cfcef4]) has_one...