简体   繁体   English

如果有别名,如何销毁依赖项?

[英]How to destroy a dependent if it is aliased?

class Team < ApplicationRecord

end
class Game < ApplicationRecord
  belongs_to :winner, class_name: 'Team'
  belongs_to :loser, class_name: 'Team'
end

Thanks in advance.提前致谢。 Is it possible to write a dependence for destroy a belonged Game instance when a Team instance is destroyed?Team实例被销毁时,是否可以为销毁所属的Game实例编写依赖项? I didn't find anything on it, all things, I tried, like我没有在上面找到任何东西,所有的东西,我试过了,比如

class Team < ApplicationRecord
  has_many :games, inverse_of: :winner, dependent: :destroy
...

didn't work.没用。

You need to add foreign keys您需要添加foreign keys

class Team < ApplicationRecord
  has_many :winner_games, foreign_key: :winner_id, class_name: 'Game', dependent: :destroy
  has_many :loser_games, foreign_key: :loser_id, class_name: 'Game', dependent: :destroy
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM