Answer the question
In order to leave comments, you need to log in
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
Try the paranoia gem , it should work for you, or choose another suitable one on ruby-toolbox
before_save do
subscribers.update_all(deleted: true) if deleted && deleted_changed?
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question