Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating associations without foreign keys #179

Open
bsotiroff opened this issue Feb 10, 2020 · 0 comments
Open

Creating associations without foreign keys #179

bsotiroff opened this issue Feb 10, 2020 · 0 comments
Labels

Comments

@bsotiroff
Copy link

While importing a CSV to create users, I'd like to add associations to records on other models. I have looked at this solution:

#68

This solution works if you have a foreign key to relate your data. However, if I do not have foreign keys on the Model I'm importing to(in my case User), I cannot use this method. It relies on being able to rename the field, to the foreign key and replacing that value.

I see the problem as coming down to the fact that:

  • the CSV is accessed in before_batch _import hook
  • the newly created models can only be modified(associated with other records) in the after_batch_import hook,
  • There is no way to transfer this information from the before/import block other than to use headers_rewrites ( and thus the renaming of an input to a different field on your user model)

I came up with a solution in which the batch_size is set to 1, and instance variables carry over the association information to the after block for the associations to be made. This doesn't seem ideal because I'm forcing the batch to be 1 record and not leveraging any of the bulk import functionality.

What I would like to be able to do is to use information on the csv to make associations in the after_batch_import block. I would like to be able to loop through the csv lines, look up the record belonging to that line, and associate the user using that information.

The batch_slice method would need to prevent the use of these columns in the before block, but also allow them to be accessed in the after_batch_import block.

Does this seem like a plausible solution? Is there a better work around to solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants