All threads / How do I add multiple subscriptions to one user ?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I add multiple subscriptions to one user ?

Arnas Klasauskas asked in General

I'm able to assign multiple subscriptions to a user, but the problem is that everytime I assign a new subscription to a user, I'm overriding the subscription_id.

I can still show the user all his subscriptions through Stripe::Subscription.list(customer: current_user.stripe_id), but it's not possible to show a specific subscription, just the last one that was created.

How can I overcome this ?

Hey Arnas,

Try changing it so you have a Subscription belongs_to :user and a User has_many :subscriptions. Then use the association to handle the subscriptions in your app.

Hey Chris,
I'm currently working without a Model, therefore it's a bit tricky, but I guess I'll be able to handle this.

Just this last step isn't working fully to my intention...

if current_user.subscription_id?
Stripe::SubscriptionItem.create(
subscription: current_user.subscription.id,
plan: plan.id
)
else
subscription = customer.subscriptions.create(
plan: plan.id
)
current_user.update(
stripe_id: customer.id,
subscription_id: subscription.id
)
end

This works when I add a new plan to the subscription, but it throws an error when I subscribe to the same plan twice. Stripe::InvalidRequestError (Cannot add multiple subscription items with the same plan: XXXXXX

Yeah, you can't subscribe to the same thing twice. You can use quantities if you need something like that.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,909+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.