我正在嘗試處理Invoice.created的Stripe Webhook並希望保存發票行項目。我的挑戰是文件變量根據訂單項類型而改變。如何排除Rails中update_attributes的空值
我試圖導入訂單項時收到,因爲根據訂單項的類型,計劃對象可能爲空。
我能夠通過將update_attributes分成2來解決問題,只有當計劃對象存在時纔會發生。以下是我的工作。我希望有更好的辦法。
@invoice_line_item = InvoiceLineItem.where(stripe_line_item_id: line_item.id).first_or_create(invoice_id: @invoice.id)
@invoice_line_item.update_attributes(
amount: line_item.amount,
currency: line_item.currency,
period_start: Time.at(line_item.period.start).strftime("%m-%d-%Y"),
period_end: Time.at(line_item.period.end).strftime("%m-%d-%Y"),
proration: line_item.proration,
item_type: line_item.type)
if line_item.plan.present?
@invoice_line_item.update_attributes(
plan_name: line_item.plan.name,
plan_interval: line_item.plan.interval,
plan_amount: line_item.plan.amount,
trial_period_days: line_item.plan.trial_period_days)
end
計劃對象是來自條帶的散列的一部分。我的應用中沒有計劃模型。當您對訂閱計劃收費時,該計劃將被分段使用。如果您爲非訂購費用計費,則散列中的計劃對象爲空。現在嘗試嘗試。 – Steve 2014-11-10 00:08:04