class Agency < ApplicationRecord
has_one :branding
end
class Branding < ApplicationRecord
has_many :agencies
end
当我销毁任何品牌时,它仍然是代理商的关键,我在该代理商中创建了branding_id
字段。
我想要某种在产品销毁过程中将其销毁的产品将其无效。
它将自动将代理商branding_id
更新为null
。
答案 0 :(得分:1)
Rails提供了此选项,请在下面进行检查,它将在代理商中将ID更新为null。有关更多信息,请检查this
class Branding < ApplicationRecord
has_many :agencies, dependent: :nullify
end
答案 1 :(得分:1)
首先,如果<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<div class="text-center">
<div id="clockdiv">
<div>
<span class="days" id="day"></span>
<div class="smalltext">Days</div>
</div>
<div>
<span class="hours" id="hour"></span>
<div class="smalltext">Hours</div>
</div>
<div>
<span class="minutes" id="minute"></span>
<div class="smalltext">Minutes</div>
</div>
<div>
<span class="seconds" id="second"></span>
<div class="smalltext">Seconds</div>
</div>
</div>
</div>
<script src="main.js" async defer></script>
</body>
</html>
模型具有Agency
列,则它应具有branding_id
而不是belongs_to
并提供has_one
选项来制作{{1 }}不需要关联:
optional: true
第二,要执行此操作,应使用branding
选项,如下所示:
class Agency < ApplicationRecord
belongs_to :branding, optional: true
end