store_model icon indicating copy to clipboard operation
store_model copied to clipboard

Add parent tracking for array attributes

Open DmitryTsepelev opened this issue 6 years ago • 3 comments

More details to be added, reference is here

DmitryTsepelev avatar Sep 19 '19 15:09 DmitryTsepelev

using #parent inside of array attribute worked for me. Is this issue outdated? 🤔

ivandenysov avatar May 07 '20 08:05 ivandenysov

Hi @ivan-denysov! If I recall correctly, the issue was related to updates (from this comment):

... direct StoreModel's Array mutation, it won't update a parent until one of the accessor methods gets called

In other words, it might not work if you take and array and add the element to it, something like:

config = Configuration.new
product.configurations << config
config.parent # => nil
product.configurations
config.parent # => Product

DmitryTsepelev avatar May 07 '20 08:05 DmitryTsepelev

Hey,

Has anything changed regarding this issue?

I just ran into a situation where I'll be needing a reference to the parent of a StoreModel in an array.

(I have a large JSON document with references between arrays. And want to create easy access to these references. For example:)

{
  foos: [
    { id: '...', bar_id: '' }
  ],
  bars: [
    { id: '...' }
  ]
}

configuration.foos.first.bar

If this behavior still isn't correct for nested store models in arrays I might be able to free up an afternoon and take a crack at fixing this.


Semi related: Is there any documentation on the #parent behavior of a StoreModel? I'm mostly wondering how to "set" the parent in situations like a test, so I could mock out the parent object, or at least supply a very small test double to play the parent role.

jessevdp avatar May 31 '23 10:05 jessevdp