E
E
eldar_web2016-03-30 12:14:26
Ruby on Rails
eldar_web, 2016-03-30 12:14:26

How in Ruby on Rails, when saving a record of one model, save all records associated with this model?

For example, there is a User model with a Subscriber: That is, when a user record is deleted, the related records from the subscriber are deleted. But I have a task - not to delete anything from the database, but simply mark it as deleted. And for this, I added an additional field to all models - . So right now, instead of deleting the user entry, I just me , that is, there will be an event not of deletion, but of saving. The question arises, earlier, when deleting a user record, subscriber records were automatically deleted. And now how can you make it so that when you save the user, that is, when you change , this happens in the subscriber (what is the best way to do it) ???
has_many :subscribers, dependent: :destroy
deleted (false или true)
deleted: false на true
deleted:false на true

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Safonov, 2016-03-30
@aleksandrsafonov

Try the paranoia gem , it should work for you, or choose another suitable one on ruby-toolbox

C
CorbenDallass, 2016-03-31
@CorbenDallass

before_save do
  subscribers.update_all(deleted: true) if deleted && deleted_changed?
end

Note that when using update_all , the callbacks of the corresponding mutable objects are not called. Need callbacks - save one by one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question