dependent-destroy

Handle dependent destroy via active jobs

天大地大妈咪最大 提交于 2020-01-14 09:35:07
问题 I have a couple models with many children. Dependent destroy has gotten really heavy. Anyone know of a way to tie dependent destroy into active jobs? Or, is my only option to remove dependent destroy and role my own jobs via callbacks on the parent model? 回答1: You can create a worker to destroy the models async and enqueue it's deletion. Something like: class ComplexModelDeletion < ActiveJob::Base def perform(model) model.destroy! end end And the model could be something like: class Model <

has_one/has_many with dependent destroy but using a different name for the key

岁酱吖の 提交于 2020-01-05 11:59:13
问题 So I'm looking at someone's code which has the following (paraphrased): class user has_one :connection, :dependent => :destroy has_one :second_user, :through => :connection, :class_name => 'User' end class connection belongs_to :user belongs_to :second_user, :class => 'User' end If I have a connection object and delete the associated 'user' it can be destroyed fine. But I also want to make it so that if the User occupying the 'second_user' field is destroyed the connection is destroyed. How

Rails dependent destroy error

家住魔仙堡 提交于 2019-12-11 15:47:18
问题 I have a Rails Movie app. With, obviously, a movie table. A movie has_many :comments, :dependent => :destroy and a comment belongs_to :movie . A comment also belongs_to :user so when a new User comments on a movie, that comment will display on their users#show page. If a User Comments on a Movie , the comment will display on their page. I can also go to localhost:3000/comments/:id to see that comment's show page Now my problem is this: If I then destroy or delete that movie with that comment,

rails prevent deletion of child unless parent is being deleted also

我是研究僧i 提交于 2019-12-10 03:06:03
问题 in Ruby on Rails 4, let's say a parent has many children. When the parent is deleted, the children must also be deleted. Other than that, the child shall not be deleted unless it is an orphan. How to do that? I tried with the following class Parent < ActiveRecord::Base has_many :children, inverse_of: :parent, dependent: :destroy end class Child < ActiveRecord::Base belongs_to :parent, inverse_of: :children before_destroy :checks private def checks not parent # true if orphan end end With the

Rails dependent which options are possible?

断了今生、忘了曾经 提交于 2019-12-08 16:22:22
问题 I get the following error in Rails 4 dependent option must be one of destroy delete apparently https://github.com/rails/rails/issues/3458 other options were supported before. But what is possible nowadays? I could not find any other documentation thank you for your help 回答1: Docs are available here Looks like the following options are supported: :destroy - causes all the associated objects to also be destroyed. :delete_all - causes all the associated objects to be deleted directly from the

What Does Rails Do With Both :dependent => :destroy and cascade delete/nullify/restrict

落爺英雄遲暮 提交于 2019-12-05 13:32:09
问题 I'm trying to decide how best to set up (if at all) foreign key constraints for my rails application. I have a model Response that belongs_to a Prompt . I would like to use :dependent => :destroy to have destroy called on every Response that belongs to a deleted Prompt and I'm trying to decide what delete constraint I should place on my foreign key. In short I want advice about how I can get best take advantage of both the destroy method on dependent objects and foreign key constraints to

What Does Rails Do With Both :dependent => :destroy and cascade delete/nullify/restrict

狂风中的少年 提交于 2019-12-04 00:22:22
I'm trying to decide how best to set up (if at all) foreign key constraints for my rails application. I have a model Response that belongs_to a Prompt . I would like to use :dependent => :destroy to have destroy called on every Response that belongs to a deleted Prompt and I'm trying to decide what delete constraint I should place on my foreign key. In short I want advice about how I can get best take advantage of both the destroy method on dependent objects and foreign key constraints to ensure cruft doesn't accumulate and reflect the logical structure of the data being stored. Several