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.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

has_one through with new value nil

#698

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.

Reported by Daniel Guettler · July 24th, 2008 @ 08:06 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. josh
    josh
    • State changed from new to stale

    Staling out, please reopen if this is still a problem.

    October 28th, 2008 @ 04:32 PM

  2. Daniel Guettler
    Daniel Guettler

    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
    

    October 28th, 2008 @ 06:38 PM

  3. Ben Woodcroft
  4. John
    John

    I can confirm this too.

    January 6th, 2009 @ 05:29 PM

  5. Daniel Guettler
    Daniel Guettler

    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?

    January 6th, 2009 @ 10:27 PM

  6. Tarmo Tänav
    Tarmo Tänav
    • State changed from stale to open

    January 7th, 2009 @ 07:09 AM

  7. Daniel Guettler
    Daniel Guettler

    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?

    January 7th, 2009 @ 07:27 AM

  8. Daniel Guettler
    Daniel Guettler

    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

    January 27th, 2009 @ 01:41 AM

  9. Repository
    Repository
    • State changed from open to resolved

    (from [4cd40726eb10fbab269be53c27b304c728541dff]) has_one :through should not create a new association when assigned nil [#698 has_one through with new value nil state:resolved]

    Signed-off-by: Pratik Naik pratiknaik@gmail.com
    http://github.com/rails/rails/commit/4cd40726eb10fbab269be53c27b304...

    May 17th, 2009 @ 01:54 PM

  10. Repository