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.

fields_for nested fubars index

#2401

using fields_for with a nested attribute numbers the resource index incorrectly. although this does not cause an exception it does make scripting js to add additional form elements harder and more error prone than it needs to be since the form elements generated do reflect the size of the actual association.

Reported by ara.t.howard · April 2nd, 2009 @ 10:11 PM

State: resolved
Milestone: 2.3.4
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. José Valim
    José Valim

    +1

    I've tested and this works well.

    I just recommend you to put some parenthesis in your patch, to agree with Rails code:

    def nested_child_index(association_name)

    April 2nd, 2009 @ 10:31 PM

  2. ara.t.howard
  3. ara.t.howard
    ara.t.howard
    • Tag changed from fields_for, nested to fields_for, nested, patch

    April 2nd, 2009 @ 10:48 PM

  4. Eloy Duran
    Eloy Duran

    Will test this out later today (evening for me).

    April 3rd, 2009 @ 08:40 AM

  5. ara.t.howard
    ara.t.howard

    thanks a bunch. i'm running patched here and it's a-ok.

    April 3rd, 2009 @ 03:45 PM

  6. Eloy Duran
    Eloy Duran
    • State changed from new to verified

    The change is good, however I have a few questions about the patch and took the liberty to rewrite it with these notes.

    • There was no test for the case where the association would be passed in as a string and a symbol. However, the code seemed to indicate that you were, rightfully so, thinking of this case.
    • Why did you choose to use "ensure"? There was at least no test which indicated why afaik.
    • The test name did not really describe what it was gonna test.

    Thoughts?

    April 3rd, 2009 @ 08:08 PM

  7. ara.t.howard
    ara.t.howard

    There was no test for the case where the association would be passed in as a string and a symbol. However, the code seemed to indicate that you were, rightfully so, thinking of this case.

    well, it isn't the case that the association should be a symbol or a string - it could be anything that responds to #to_s, a custom class, a stringio, etc. that's why there is no test for symbol explicitly but simply ducktype to string no matter what's passed in. for instance ['posts'] would work and so would [:posts]. this is useful in a *args case. however, testing all possible stringlike intputs seems unreasonable.

    Why did you choose to use "ensure"? There was at least no test which indicated why afaik.

    it's there because ruby lacks a prefix increment function (++i). the previous code uses the obfusication pattern of initializing with -1 and using +=, but that really doesn't read 'starts at zero and increments by 1'. ensure is a good way to implement prefix increment in ruby since it increments on the way out of the method, after the return value has been determined.

    The test name did not really describe what it was gonna test

    guilty as charged. ;-) not for lack of trying though!

    thanks for the work on this - it really makes adding elements via js vastly simpler.

    April 3rd, 2009 @ 08:22 PM

  8. ara.t.howard
    ara.t.howard

    oh crap. should have read 'Formatting help'. sorry bout that.

    April 3rd, 2009 @ 08:23 PM

  9. Eloy Duran
    Eloy Duran

    well, it isn't the case that the association should be a symbol or a string - it could be anything that responds to #to_s, a custom class, a stringio, etc. that's why there is no test for symbol explicitly but simply ducktype to string no matter what's passed in. for instance ['posts'] would work and so would [:posts]. this is useful in a *args case. however, testing all possible stringlike intputs seems unreasonable.

    True, I changed it back to #to_s again. But adding at least one different object in the test is at place.

    it's there because ruby lacks a prefix increment function (++i). the previous code uses the obfusication pattern of initializing with -1 and using +=, but that really doesn't read 'starts at zero and increments by 1'. ensure is a good way to implement prefix increment in ruby since it increments on the way out of the method, after the return value has been determined.

    Fair enough, I changed this back. Thanks for the trick ;-)

    oh crap. should have read 'Formatting help'. sorry bout that.

    Yeah the lack of a preview on LH doesn't help either…

    guilty as charged. ;-) not for lack of trying though! thanks for the work on this - it really makes adding elements via js vastly simpler.

    Yes definitely, thanks again.

    April 3rd, 2009 @ 08:58 PM

  10. Eloy Duran
    Eloy Duran
    • Assigned user changed from Eloy Duran to Michael Koziarski

    April 5th, 2009 @ 04:18 PM

  11. DerGuteMoritz
  12. Jarl Friis
    Jarl Friis

    May I suggest a slightly different patch for this problem. Take a look at the patch.

    It uses the local string 'name' as key. It is more informative, but it may be slower, since it is not a symbol.

    May 11th, 2009 @ 01:15 PM

  13. ara.t.howard
    ara.t.howard

    checkout my initial patch, it did exactly this. the concept simply got dropped when others hacked on it.

    May 11th, 2009 @ 04:24 PM

  14. Jarl Friis
    Jarl Friis

    @ara.t.howard

    Your initial patch uses "association_name" as argument to nested_child_index, whereas I suggest (in my patch) to use the local variable called "name" as argument to nested_child _index.

    I fear that if not using a fully qualified parameter name (such as "name"), then using a unqualified value like association_name will eventually turn up again further down the parameter structure. If you are interested I will try to make a test that fails that illustrates the problem.

    May 12th, 2009 @ 11:12 AM

  15. Eloy Duran
    Eloy Duran

    @Jarl You're right, it's a good improvement. Please do add the failing test you are talking about.

    May 12th, 2009 @ 11:16 AM

  16. Jarl Friis
    Jarl Friis

    As per request by Eloy Duran.

    I have changed the test (initially made by ara.t.howard) to a stricter test testing for the case where sub-associations have same names.

    Now the test fails with the initial patch, but passes with my patch.

    May 13th, 2009 @ 02:17 PM

  17. Michael Koziarski
    Michael Koziarski
    • Milestone changed from 2.x to 2.3.4

    Is this good to go eloy?

    June 9th, 2009 @ 09:16 AM

  18. Repository
    Repository
    • State changed from verified to resolved

    July 2nd, 2009 @ 08:46 PM

  19. Repository