E
E
eldar_web2017-02-15 17:52:14
Ruby on Rails
eldar_web, 2017-02-15 17:52:14

How to do rollbacks in Ruby on Rails in case of bad situations?

Let's say there are models:
Subscriber (subscriber)
Subscription (subscription)
Period (subscription periods).
All of them are interconnected, that is, a subscriber can have several subscriptions, and subscriptions can have several periods.
And when adding a subscriber, there must be a subscription, and the subscription has a period.
So when adding in the controller, it is usually added like this.
First subs = Subscriber.create()
Then subscription = Subscription.create(subscriber_id: subs.id)
period = Period .create(subscription_id : subscription.id).
Question: what if the Subscriber is added, but the rest are not (for example, the Internet disappeared immediately after adding the Subscriber), because all of them must be mandatory?
Yes, you can write "use transactions", but this concept is loose. Could you give an example?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karim Kyatlottyavi, 2017-02-16
@eldar_web

Frankly speaking, I do not see the need for manual use of the transaction here. If the client has already sent the form and the server has received the data, then the form data will be processed by the rail, regardless of who lost the Internet. I would recommend doing something like this (I think this code is more ideologically correct):

subscriber = Subscriber.new
subscription = sub.subscriptions.build
subscription.periods.build
subscriber.save

This code will indeed require properly configured links.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question