rubyruby-on-rails-3.2mailchimpruby-1.9.3gibbon

Gibbon Update EMail


I'm starting a sidekiq-job in background which updates the users newsletter_info async in background via after_commit callback. So if a user updates his language in his settings, the language got updated in MailChimp too. Everything works fine except the emailadresse. If a users updates his emailadresse it will be shown in the log messages and it will be at the right place but it will not be updated in mailchimp. Instead a new user with the new emailaddress gets adeed to the mailchimp list...

So one option might be to delete the old one, but i think that solution is not clean.

My other thought is that maybe the emailadresse is although the subscriber id and there the error might be born.

Thank you very much for your help,

Regards, Khaled.

def update_newsletter_info(args)
  NDC.push "KHALED -- "
  Rails.logger.debug "UPDATENEWSLETTERINFO ARGS: #{args}"
  XIXI::Workflow.for(TwitterUsersMaintainer, args) do |workflow, user|
    Rails.logger.debug "INFOS IN UPDATENEWSLETTERINFO #{user.locale} #{user.email}"
    gb = Gibbon::API.new
    cfg = XOXO::Mailchimp.config

    gb.lists.subscribe(
      :id    => cfg.list_id,
      :email => { :email => user.email },
      :merge_vars => {
        :mc_language => user.locale,
        :groupings => [
          { name: cfg.group_name, groups: [ user.recurring_payment? ? 'basic' : 'free' ] }
        ]
      },
      :double_optin    => !!cfg.double_optin,
      :update_existing => true,
      :send_welcome    => !!cfg.send_welcome
    )

    # continue
    workflow.continue_with(user)
  end
  NDC.pop
end

Solution

  • The answer to my question is: I already mentioned that the email address could although be the id in the mailchimp subscriber list. And so it was.

    So I used this code in the model to unsubscribe the old email adress and add the new user to the subscriberlist:

    if self.previous_changes.include?("email")
          email = self.previous_changes[:email][0]
          UsersMaintainer.perform_async( action: :unsubscribe_newsletter, email: email )
        end
        UsersMaintainer.perform_async( action: :update_newsletter_info, user_id: self.id )
    

    and add the new member to the list.

    Maybe it's not the best way but it is working! ;)

    Thanks to anybody who has read the question!