activerecord-import icon indicating copy to clipboard operation
activerecord-import copied to clipboard

belongs_to isn't processed in import

Open majioa opened this issue 7 years ago • 8 comments

I seems that belongs_to relation isn't proceeded by import during commit, my log shows that import inserts the only has_one/many relations. How can it be fixed?

majioa avatar Jul 29 '16 18:07 majioa

Is there a reason why you can't start the import from the model associated using belongs_to, basically going up one more level?

jkowens avatar Jul 29 '16 19:07 jkowens

@jkowens yes, because that models isn't at the top of the import trees. And I start the proceeding from a models, which is on the top of the import tree.

majioa avatar Aug 01 '16 12:08 majioa

@jkowens example: Tags has_many Posts, Posts belongs_to Types, i need save tags with posts and types

skrinits avatar Aug 19 '16 09:08 skrinits

Gotcha. Unfortunately I think "recursive" import will have to be limited to has_many associations, otherwise we'd end up with endless cycles.

So in the example above, types would have to be imported first and then tags with posts.

jkowens avatar Oct 07 '16 17:10 jkowens

@jkowens If you will properly process the has_many/belongs to pairs it will not lead to recursion, because it will not walk over already proceeded relations,

majioa avatar Oct 10 '16 16:10 majioa

That's true, I suppose a hash with keys for each class could be added that holds all imported associations. Then when selecting associated models to import we could first check to see if it exists in the array for that class in the processed hash.

I assume the memory overhead wouldn't be significant. Do you think you'd be able to work on a patch for this feature?

jkowens avatar Oct 10 '16 18:10 jkowens

It would also be helpful if you could provide some benchmarks for recursive imports with just has_many associations before and after the change.

That way it would be easier to determine if the benefits outweigh the costs (performance, complexity, etc).

jkowens avatar Oct 10 '16 18:10 jkowens

Importing belongs_to associations is still not supported recursively, but here is an example how to possibly handle such a situation for now:

See this thread: https://github.com/zdennis/activerecord-import/issues/402#issuecomment-328375171

jkowens avatar Sep 10 '17 22:09 jkowens