algoliasearch-rails
                                
                                 algoliasearch-rails copied to clipboard
                                
                                    algoliasearch-rails copied to clipboard
                            
                            
                            
                        Rework enqueuing system to ensure records got deleted accordingly
Hi, I tried this snippet of code as suggested in the readme for enqueuing Algolia operations :
class Contact < ActiveRecord::Base
  include AlgoliaSearch
  algoliasearch enqueue: :trigger_delayed_job do
    attribute :first_name, :last_name, :email
  end
  def self.trigger_delayed_job(record, remove)
    if remove
      record.delay.remove_from_index!
    else
      record.delay.index!
    end
  end
end
My application is running rails 3.2.22 with ruby 2.2.2 and DelayedJob 4.0.6
Without delayed job, the auto_remove works perfectly fine. However, when I try to remove the project with the enqueuing system set, I get the following error :
ArgumentError:
       job cannot be created for non-persisted record:
after further research, I found this DJ related issue : https://github.com/collectiveidea/delayed_job/issues/820 Is there any workaround to this, since I want to remove my model asynchronously from Algolia only after my project has been deleted
That's weird, do you have the associated backtrace? Is it raised from the record.delay.{remove_from_,}index! methods?
Yes it is raised from record.delay.remove_from_index! When I destroy my model. For example : project.destroy
@redox Here is an example of a failing spec backtrace :
 Failure/Error: expect { delete :destroy, :id => @project }.to change(Project, :count).by(-1)
     ArgumentError:
       job cannot be created for non-persisted record: #<Project
Can you just try to run a record.remove_from_index! and another record.delay.remove_from_index! from the rails console, to check if you're experiencing the same? I'm don't get it right now :/
record.remove_from_index! works whereas record.delay.remove_from_index! doesn't.
Actually, it doesn't work only after I destroy the record and the self.trigger_delayed_job is triggered.
Since it's a "remove" it steps in the if remove condition, and finally the exception I told you about above is raised.
When I try without the "delay" it works whereas with the delay it doesn't and it displays this error :
ArgumentError:
       job cannot be created for non-persisted record: #<Project
Could you just try with another (non-algolia) method? From what I understand, your Project object cannot be serialized but I don't get why it could be related to the remove_from_index!.
I am only using what's suggested in the readme for delayedjob.  record.delay.remove_from_index! works outside the self.trigger_delayed_job, if I try it in the console. However when I try it after a destroy,  for instance :  project.destroy, then as I explained,record.delay.remove_from_index! does not work anymore.
Could you try that to see maybe you have to update the example you're suggesting in the readme ?
+1. I am having the same problem.
I ran into the same issue. The workaround I've used is to delete the record by ID from the index in the background job. It seems to work fine
   def trigger_delayed_job(record, remove)
      if remove
        index = Algolia::Index.new("#{self.name}_#{Rails.env}")
        index.delete_object(record.uuid)
      else
        obj = self.find(record.id)
        obj.index!
      end
    end