This project is archived and is in readonly mode.

#1941 ✓resolved
pete_lacey

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

Reported by pete_lacey | February 11th, 2009 @ 03:34 PM | in 2.x

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...

Comments and changes to this ticket

  • Gaspard Bucher

    Gaspard Bucher February 11th, 2009 @ 04:28 PM

    I could reproduce your example => same error.

    I tried to reproduce it with the patch for #1930 (better validations for autosave) => no error.

    The last patch solves your problem: #1930.

  • pete_lacey

    pete_lacey February 11th, 2009 @ 05:43 PM

    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 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.

  • pete_lacey

    pete_lacey February 11th, 2009 @ 06:39 PM

    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.

  • Eloy Duran

    Eloy Duran February 17th, 2009 @ 08:35 AM

    • Assigned user set to “Eloy Duran”
  • Michael Koziarski

    Michael Koziarski February 22nd, 2009 @ 02:22 AM

    • State changed from “new” to “resolved”

    Duplicate of #1930 if that patch fixes it

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>

Referenced by

Pages