representable icon indicating copy to clipboard operation
representable copied to clipboard

Fix populator bug causing new data to never be set.

Open undergroundwebdesigns opened this issue 9 years ago • 4 comments

Currently, the populator is coded to remove the SetValue operation from the pipeline, and in my testing this means that the populator runs and returns the correct record (e.g. via the "find or instantiate" logic), but then new values from the are never set on that record.

In my testing, simply dropping this line makes everything work as expected.

I do see that Representable::FindOrInstantiate is coded to set it's result object on the represented object directly, so maybe I'm misunderstanding something about how populators are supposed to work? As currently coded, how are the input values ever going to be set on the populated object?

undergroundwebdesigns avatar Oct 13 '16 15:10 undergroundwebdesigns

Note: I also dropped the "setter" lines from the Representable::FindOrInstantiate lambda, as they seem to now be redundant and caused the value to be set twice. Usually this isn't a problem, but in certain cases with rails "has and belongs to many through" relations, setting the relation the second time causes an undesired DB save ¯_(ツ)_/¯ (Rails, why you do dis!?!)

undergroundwebdesigns avatar Oct 13 '16 21:10 undergroundwebdesigns

The input values in the nested fragment are set after the populator. The latter creates/finds the nested object, appends it to the parent object (and you're right, I am confused now, too, about why you could simply remove that block), and then calls from_json for the representer corresponding to the nested fragment.

apotonick avatar Dec 03 '16 14:12 apotonick

Well, if we're both confused, maybe some more research is needed into what's going on here? I can confirm the above fix works, we've been using it in production on a rather large app for a couple months now...

undergroundwebdesigns avatar Dec 05 '16 13:12 undergroundwebdesigns

Thanks Alex, I am about to dive into making Reform's and Representable's populators do the same thing, I'll let you know about my findings. Thanks for pointing this out!!!

apotonick avatar Dec 05 '16 13:12 apotonick