【发布时间】:2016-05-03 16:01:34
【问题描述】:
我有以下场景
class Company < ActiveRecord::Base
has_many :depots, inverse_of: :company, :dependent => :destroy
has_many :users, inverse_of: :company, :dependent => :destroy
has_many :products
end
class Depot < ActiveRecord::Base
has_many :products, :dependent => :destroy
has_many :zones, :dependent => :destroy
belongs_to :company
end
class Product < ActiveRecord::Base
belongs_to :depot
belongs_to :company
end
class Zone < ActiveRecord::Base
belongs_to :depot
end
class User < ActiveRecord::Base
belongs_to :company, , inverse_of: :users
end
## and zone has_many locations which further has some associated models.
我想删除所有与公司关联的模型,而不是一一调用。 我的依赖销毁不起作用,当我尝试删除公司时,产品仍然存在。我试过替换
dependent: :destroy #to delete_all
但没有运气。如何通过一次对公司的 destroy 删除所有嵌套对象?
编辑
我可以打电话
Company.reflect_on_all_associations(:has_many)
并一一删除所有关联,但我不想采用这种方法。有什么帮助吗??
【问题讨论】:
-
请发布以下 MySQL 查询的结果:“SHOW CREATE TABLE Companies”
-
抱歉,我使用 Postgres 作为我的数据库。
标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-3.2 associations has-many