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.

reload inconsistent in AR

#5559

ActiveRecord::Base#reload returns the record or throws ActiveRecord::RecordNotFound.
But AssociationProxy#reload just catches this error and returns nil.

I think its a huge problem that a record fetched through an association behaves different as a plain record. See test for example.

This hit me hard while implementing a spec which checked if some records would be destroyed with this code:

 lambda { entity.reload }.should raise_error(ActiveRecord::RecordNotFound)
 # entity was fetched through an association so reload returned nil

Reported by Kane · September 5th, 2010 @ 05:27 PM

State: wontfix
Milestone: none
Assigned to: Rohit Arondekar Rohit Arondekar
Importance: Low

Activity

  1. David Trasbo
    David Trasbo

    Whether or not that's inconsistent is rather subjective. Of course, if you try to reload a Active Record object that doesn't exist in the database anymore that should cause an error and it does.

    But personally I'd be very surprised to see an error when reloading an empty association. I definitely think this is the intended behavior. Why don't you just check to see if #reload returns nil?

    September 5th, 2010 @ 06:43 PM

  2. David Trasbo
    David Trasbo
    • Assigned user set to Rohit Arondekar

    I think this can be marked wontfix.

    September 9th, 2010 @ 08:12 AM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to wontfix
    • Importance changed from to Low

    October 6th, 2010 @ 05:41 AM