E
E
eldar_web2016-01-01 20:58:42
Ruby on Rails
eldar_web, 2016-01-01 20:58:42

How in Ruby on Rails to rollback an add if its related ones fail?

For example, there are Subscriber and Address models and they are interconnected, that is, Subscriber is added first, and then Address is added with the Subscriber id saved.
That is something like this, in the controller:

def create
subs = Subscriber.create(...)
Address.create(subscriber_id: subs.id...)
end

Here, sometimes it can happen that the Subscriber can be added, but the Address is not (for example, the Internet is gone).
Indeed, in this case, Subscriber will be, but there will be no Address.
How to make it so that if the Address is not added, then the created Subscriber is also deleted?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2016-01-01
@eldar_web

Use transactions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question