首页 > 解决方案 > 添加对现有活动记录的引用并添加belongs_to,破坏代码

问题描述

我添加了对现有模型的引用:

def change
   add_reference :price_change_requests, :price_reminder, foreign_key:true
end

然后添加一个属于它的关系:

belongs_to :price_reminder

并添加了相应的:

class PriceReminder < ApplicationRecord
  has_many :price_change_requests
end

但是现在我的代码在运行时会中断。但是,如果我注释掉#belongs_to :price_reminder

代码再次起作用

可能是什么问题呢?

标签: ruby-on-rails

解决方案


我的错!忘了补充:

optional: truebelongs_to

完整的:belongs_to :price_reminder, optional: true


推荐阅读