A
A
Anton Ivanov2015-05-21 04:51:41
Ruby on Rails
Anton Ivanov, 2015-05-21 04:51:41

Is it possible in rails to override both save and save! with one function?

Hello.
Is it possible in rails to override both saveand save!for a model with a single function?
Looking at the source, I found that both of these functions call a function with_transaction_returning_status
. Is it possible to somehow hang up an event to write an object to the database, or do both of these functions need to be redefined?
Or am I going the wrong way at all? The goal is similar to the fact that, for example, when adding a post to the database, I need to add it to the user's feed in elasticsearch. At the same time, I do not want to think every time which method is called, save or save!
If it is customary to do this differently, I ask for help :)
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jeiwan, 2015-05-21
@Fly3110

www.rusrails.ru/active-record-callbacks

R
Renat Ibragimov, 2015-05-24
@MpaK999

No need to override, but just hang up calling your method on callback before_save or after_save

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question