Hi guys,
I'd like some advice on how I should synchronize a list of email addresses on 11k users against an external mailing list program, in this case http://www.mailchimp.com/api/1.2/listbatchsubscribe.func.php" title="Mailchimp API"">Mailchimp.
Normally the way I'd so this is simply to have an :after_save callback, to send a single update to the external api.
But already each hour, a rake task is run to update a property on every user in the database. If I simply did that, every hour, the the poor mailchimp API would get be hit 11,000 times.
What's the most efficient, simple way to do this, to check only if a single attribute you're watching has changed from what it was before the save?
If there's a variable that persists across the transaction lifecycle I would simply do something like this, where I check if the value has changed, and if it's different execute come other code.
class User
:before_save :store_old_email
:after_save :sync_with_chimp
def store_old_email
$ugly_of_global_variable_to_store_email = user.email
end
:sync_with_chimp
if $ugly_of_global_variable_to_store_email != user.email
//update_mail_chimp_api
end
end
end
I've checked the http://api.rubyonrails.org/" title="Rails Framework Documentation"">rails api here, and I'm still slightly unclear on how I should be doing this.
Would you use the dirty? class here to do this?