I'm puzzled by an update I'm attempting to do with Rails 4 / Activerecord. The app I'm working on is using Stripe to handle subscriptions, and this function should deal with an updated subscription (stripe customer.subscription.updated event):
# passing in a stripe webhook event object
# event.data.object.current_period_end = 1402100302
subscription = Subscription.find_by(stripe_id: event.data.object.id)
plan = Plan.find_by(stripe_name: event.data.object.plan.id)
subscription.update(expires_at: event.data.object.current_period_end, plan_id: plan.id)
This all seems clear, and when I execute the individual lines in a rails console, I get valid data for subscription, plan, etc. However, the update doesn't work as expected. Here's what the query executed in the rails console shows for the update line:
SQL (0.7ms) UPDATE "subscriptions" SET "expires_at" = $1, "updated_at" = $2 WHERE "subscriptions"."id" = 23 [["expires_at", nil], ["updated_at", Wed, 07 May 2014 17:09:25 UTC +00:00]]
(15.9ms) COMMIT
=> true
expires_at is nil, and the second value, plan.id doesn't even get passed in to the query. How should I handle this update?
# event.data.object.current_period_end = 1402100302.plan_idis a foreign key onsubscriptions. The problem is that stripe uses a different id for the plan than is stored locally, so as far as I can see, we have to look it up. - user101289