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.

autosave generates error when one-to-one association is empty after being accessed.

#1941

I'm not sure how best to phrase this issue, and it might ultimately be a problem with association proxies, but the problem I am seeing in Rails Edge (as of 2009-Feb-11) is visible when working with autosave. (Actually, I'm working with nested attributes, but they turn on autosave by default.) In a nutshell:


class Entity < ActiveRecord::Base
  has_one :person, :autosave => true
end
=> nil

class Person < ActiveRecord::Base
end
=> nil

e =  Entity.new
=> #<Entity id: nil, ... >

e.valid?
=> true

e.person
=> nil

e.valid?
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
	from /home/placey/Development/tgrii/webapp/vendor/rails/activerecord/lib/active_record/autosave_association.rb:198:in `autosave_association_valid?'
	from /home/placey/Development/tgrii/webapp/vendor/rails/activesupport/lib/active_support/core_ext/object/misc.rb:39:in `returning'
	from /home/placey/Development/tgrii/webapp/vendor/rails/activerecord/lib/active_record/autosave_association.rb:197:in `autosave_association_valid?'
	from /home/placey/Development/tgrii/webapp/vendor/rails/activerecord/lib/active_record/autosave_association.rb:184:in `valid?'
	from (irb):64:in `all?'
	from /home/placey/Development/tgrii/webapp/vendor/rails/activerecord/lib/active_record/autosave_association.rb:179:in `each'
	from /home/placey/Development/tgrii/webapp/vendor/rails/activerecord/lib/active_record/autosave_association.rb:179:in `all?'
	from /home/placey/Development/tgrii/webapp/vendor/rails/activerecord/lib/active_record/autosave_association.rb:179:in `valid?'
	from (irb):64

In looking around (amateurishly) it seems the problem is that after accessing the 'person' association, the #loaded? method on the has_one association proxy is returning true even though nothing is loaded.

This sounds somewhat like this old issue: http://dev.rubyonrails.org/ticke...

Reported by pete_lacey · February 11th, 2009 @ 03:34 PM

State: resolved
Milestone: 2.x
Assigned to: Eloy Duran Eloy Duran
Importance: none

Activity

  1. Gaspard Bucher
    Gaspard Bucher

    I could reproduce your example => same error.

    I tried to reproduce it with the patch for #1930 autosave should validate associations even if master is invalid (better validations for autosave) => no error.

    The last patch solves your problem: #1930 autosave should validate associations even if master is invalid.

    February 11th, 2009 @ 04:28 PM

  2. pete_lacey
    pete_lacey

    Gaspard,

    Thanks! I thought that patch would help too, and tried it before reporting. Unfortunately, it's mutually incompatible right now (from a Git point of view at least) with the patch for #1892 nested attributes should not have meaningful hash keys which I also need. So I was unable to apply it. I guess I'll wait for both patches to be rebased and committed to trunk. Maybe there's another way, but I'm a Git newb.

    Thanks again.

    February 11th, 2009 @ 05:43 PM

  3. pete_lacey
    pete_lacey

    Final note. I got a little smarter about Git and was able to apply both patches. I can now confirm this works and works in conjunction with nested attributes.

    Thanks, Gaspard.

    February 11th, 2009 @ 06:39 PM

  4. Eloy Duran
    Eloy Duran
    • Assigned user set to Eloy Duran

    February 17th, 2009 @ 08:35 AM

  5. Michael Koziarski