0

So I'd like to add a user that registers on my rails site onto a mailing list. I've got the gibbon gem installed and I had success testing my ruby method in a stand-alone ruby file. I'm having issues integrating it into my application, however.

The error I receive in my rails logs is ERROR -- : NotImplementedError (NotImplementedError)

Here's my approach, which I tried to follow from this blog post: http://aspiringwebdev.com/mailchimp-and-active-job-on-rails-4-adding-users-to-your-mailing-list-in-the-background/

I have a background worker installed (sucker_punch).

I created a Job called AddUserToRegistrationListJob

class AddUserToRegistrationListJob < ActiveJob::Base
  queue_as :default
  require 'gibbon'

    def registration_list(user_id)
        user = User.find(user_id)
        email = user.email
        first_name = user.first_name
        last_name = user.last_name

        mailchimp_list_id = "my list id here"
        Gibbon::Request.api_key = "my api key here"
        Gibbon::Request.timeout = 15
        gibbon = Gibbon::Request.new

        gibbon.lists("#{mailchimp_list_id}").members.create(
            body: 
            {
                email_address: email, 
                status: "subscribed", 
                merge_fields: {FNAME: first_name, LNAME: last_name
                    }})
    end
end

Then in my user.rb file, I have

after_create :add_user_to_registration_list

def add_user_to_registration_list
    AddUserToRegistrationListJob.perform_later(self)
end

In my ruby file, which worked fine, I specified the argument for the method as

def registration_list(email, first_name, last_name)

and passed in the object when I ran the ruby file like this:

registration_list("email@example.com", "John", "Doe")

Wasn't clear on how to adjust the method or the arguments to accommodate rails. Any suggestions?

JohnOHFS
  • 174
  • 14
  • I think you should call your method perform instead of registration_list inside AddUserToRegistrationListJob – Typpex Aug 18 '16 at 04:04
  • That got it quasi-functional, but now I have this error: ActiveJob::DeserializationError Error while trying to deserialize arguments: Couldn't find User with 'id'=111. User 111 is the sample user_id assigned to the user I created to test this out. – JohnOHFS Aug 18 '16 at 04:21
  • Can you confirm the user with id 111 exists in database ? – Typpex Aug 18 '16 at 04:22
  • Yep. They do indeed exist. – JohnOHFS Aug 18 '16 at 04:24
  • I am not sure what is the problem as i dont use active job, i would say instead of passing an AR object you should just pass the ID and look for the record once in the job such as AddUserToRegistrationListJob.perform_later(self.id) then Active job won't have to do complex serialisation of AR instance – Typpex Aug 18 '16 at 04:27
  • So I actually got it to work by changing after_create to after_commit. Only issue I'm having now is the list is updated and either sucker_punch or AJ tries to push it through again and I get an error back from Mailchimp that the record already exists. Anyway to stop if from doing that? – JohnOHFS Aug 18 '16 at 04:34

0 Answers0