如何使用循环删除特定记录的所有关联记录? 当我在那时删除一个特定卖家时,应该删除他们的相关记录。 例如。当一个卖家被删除时,他们的产品,客户,卖家市场应该被删除。(不应该删除市场。)
class Seller < ActiveRecord::Base
has_many :products
has_many :customers
has_many :seller_marketplaces
has_many :marketplaces through: :seller_marketplaces
end
答案 0 :(得分:1)
你的模特应该是: -
onClick1(){
ScannerInteractor.startScan(this, null, null);
//alert('cllllll');
}
假设你有一个卖家
class Seller < ActiveRecord::Base
has_many :products
has_many :customers
has_many :seller_marketplaces
has_many :marketplaces through: :seller_marketplaces
end
答案 1 :(得分:0)
您可以使用after_destroy回调
class Seller < ActiveRecord::Base
has_many :products
has_many :customers
has_many :seller_marketplaces
has_many :marketplaces through: :seller_marketplaces
after_destroy :destroy_related_records
def destroy_related_records
#delete products
products.each do |product|
product.destroy
end
#delete customers
customers.each do |customer|
customer.destroy
end
#delete seller_marketplaces
seller_marketplaces.each do |seller_marketplace|
seller_marketplace.destroy
end
end
end