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.

has_one :through errors

#895

This patch resolves two small errors in has_one :through associations:

1) An unassigned has_one :through association should return nil when referenced. Instead, it returns an empty array. This is due to the call to #reset_target! on AssociationCollection, which is the most derived class that defines that method in the inheritance hierarchy for HasOneThroughAssociation.

2) Assigning to a has_one :through association does not correctly assign to the target of the association. This is due to the way the target gets set in the attribute writer method. The attribute writer creates the join association, then delegates to the attribute reader, with an explicit reload, to recreate the has_one :through association and save it on the instance. However, after calling the attribute reader, the attribute writer overwrites the instance variable with an out of date association object.

This patch includes tests for each of these behaviors, along with fixes.

Reported by Adam Milligan · August 25th, 2008 @ 12:47 AM

State: committed
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Michael Koziarski

    Hi there,

    There's an awful lot of whitespace changes in this patch which make it a little hard to follow. Are the two changes stand alone?

    If so it'd be nice to get seperate changesets for each of them with the whitespace changes either not included, or in a seperate changeset (as they're removing trailing whitespace I'm happy to take them).

    August 25th, 2008 @ 09:41 AM

  2. Adam Milligan
  3. Adam Milligan
    Adam Milligan

    Sorry, my editor removed the whitespace automatically. It looked like it was in line with the Rails code standards so I left it in.

    Here is a new patch without the whitespace changes.

    August 26th, 2008 @ 05:37 PM

  4. Repository
    Repository
    • State changed from new to committed

    (from [9dbde4f5cbd0617ee6cce3e41d41335f9c9ce3fd]) Fix two has_one :through errors

    • Set the association target on assignment;
    • Reset target to nil on reset, rather than empty array.

    Signed-off-by: Michael Koziarski michael@koziarski.com [#895 has_one :through errors state:committed] http://github.com/rails/rails/co...

    August 27th, 2008 @ 10:25 AM

  5. Michael Koziarski
    Michael Koziarski

    No worries, they're definitely the correct format but I try to have whitespace changes on their own to prevent people getting confused by later uses of git blame.

    August 27th, 2008 @ 10:25 AM

  6. Repository
    Repository

    (from [e710902f26a2eed7adb22082067df449b9641d00]) Fix two has_one :through errors

    • Set the association target on assignment;
    • Reset target to nil on reset, rather than empty array.

    Signed-off-by: Michael Koziarski michael@koziarski.com [#895 has_one :through errors state:committed] http://github.com/rails/rails/co...

    August 27th, 2008 @ 10:27 AM