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.

Accessor methods for association instances.

#1728

Currently the active_record/associations.rb contains code like the following:


ivar = "@#{reflection.name}"

# for reading
association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
if association.respond_to?(:loaded?)
  # ...
end

# and for writing
instance_variable_set(ivar, association)

This patch cleans this up by providing a association_instance_get and a association_instance_set method, which provide the same functionality as described above.


# for reading
if association = association_instance_get(reflection.name)
  # ...
end

# and for writing
association_instance_set(reflection.name, association)

As you can see, it's the same minus the instance_variable_defined? part. Tests have shown that this call is not needed. Please provide feedback if it it needed for a unknown reason.

Reported by Eloy Duran · January 11th, 2009 @ 01:29 AM

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

Activity

  1. Manfred Stienstra
    Manfred Stienstra

    Nice, I think we've had this refactoring floating around for around 6 months now.

    +1 Applies and tests run.

    January 11th, 2009 @ 11:41 AM

  2. Eloy Duran
    Eloy Duran

    @manfred Yup, indeed we did :) Right now I'm tearing the big patch it was part of apart, that should make it in easier.

    January 11th, 2009 @ 11:48 AM

  3. Michael Koziarski
    Michael Koziarski
    • Milestone cleared.
    • Assigned user set to Michael Koziarski

    This actually subtly changes the semantics as it doesn't do a test for instance_variable_defined?(...)

    However, that doesn't actually seem to matter...

    January 12th, 2009 @ 05:20 AM

  4. Michael Koziarski
    Michael Koziarski
    • Milestone set to 2.x

    This doesn't apply cleanly any more as someone went and added heaps of code to associations.rb for the nested params stuff :)

    February 1st, 2009 @ 02:07 AM

  5. Eloy Duran
    Eloy Duran

    This was applied in commit ec8f04584479aff895b0b511a7ba1e9d33f84067. Please close the ticket.

    February 1st, 2009 @ 01:09 PM

  6. Michael Koziarski
    Michael Koziarski
    • State changed from new to resolved

    February 1st, 2009 @ 09:43 PM