I'm trying to add a discount object to a subscription with the braintree-rails gem, but it is not applied. I'm guessing my code must be wrong, but I can't find a working example.
discount = BraintreeRails::Discount.find(params[:subscription_promo])
subscription = @plan.subscriptions.build permitted_params[:subscription]
subscription.discounts << discount
# ...
subscription.save
When I dump discount
, it is loaded properly. The subscription is created just fine, but at full price. The discount is not there. How can I add the discount to the subscription?
Update: I tried modifying the direct query, but that has not helped.
@subscription.raw_object.discounts = {add:[{inherited_from_id: discount.id}]}
Update 2: I also ran a direct Braintree request against the API with the request expected of the above code, and it worked. There's something wrong happening between setting it and saving.
Update 3: A workaround is possible by extracting the attributes of the BraintreeRails::Subscription
object, using Braintree::Subscription
to call the API, and using BraintreeRails::Subscription.find
to load it back into the object. This is definitely not optimal, though, since it's not very clean, and requires an extra API call.