Hi! I was trying to see how to implement csv imports using the book and neither apps have it included in the code. I tried to get it working using the code snippets and the book and I kept getting error undefined variable data_import. My data_import form is in my client views folder so I tried a belongs_to :client and has_many :data_imports in my Client model to see if it wasn't associating, tried instantiating it in an import controller method, tried replacing MigrationService module with just a Class and making a call to the Class. I am missing something, but not sure where to start. Anyone had luck implementing?
Yep so the way I approach this is by having everything ride off of a database record. For data imports (and exports) I'd have a model named DataImport.
The end user fills out the form, and provides the CSV file. The DataImport record is saved to the database, then at that point is when the async job kicks in and fires the service. The service object is called with the data_import record, which has the CSV file on it that the user uploaded.
So the service object example in the book assumes that the data import record exists in the database.
Does this make sense? Let me know how you go about!
Thanks! That helps me understand what you did better but brings up a few more questions so I can give it another try:
Do I need to generate a migration adding data_imports to clients?
Also, where is the callback that tells the MigrationService to run once the form is saved to the database? I don't see where the model calls the service.
You have Stripe as an import_source but don't address it in the service...is that for another import elsewhere in your code?
It looks like I need to update that screenshot in the book.
What's missing is the after_create_commit model callback, which calls DataImportJob.perform_later(data_import_id). The job then calls the service.
Sorry for the confusion on that and thanks for brining it up! I'm adding to the list of things to update for the new version.
And yeah I would have the DataImport belong to the client, so the client would has_many data_imports (client_id on data_import table).
Does this help?
Right on, that really helps! It's nice to leave the controller out of it.