ruby-on-rails - 添加对现有活动记录的引用并添加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
代码再次起作用
可能是什么问题呢?
解决方案
我的错!忘了补充:
optional: true
后belongs_to
完整的:belongs_to :price_reminder, optional: true
推荐阅读
- machine-learning - 贝叶斯岭回归和自动相关性确定回归之间的区别?
- sass - 错误:找不到或无法读取要导入的文件:node_modules/bootstrap/
- spring-batch - 如何在没有作业参数的情况下临时运行弹簧批处理作业?
- laravel - 使用同步而不是数据库时出现 504 网关超时
- java - 仅获取 TCP 客户端发送的问题
- sockets - websockets 与 TCP 和 UDP 有何不同?
- powershell - 禁用在 PowerShell 中调用命令后打印的横幅消息
- ios - 如何修复 [!] 在 `.symlinks/plugins/flutter_test/ios` 中找不到 `flutter_test` 的 podspec
- spring - Spring env,我是否仍应在新项目中使用 Hystrix
- node.js - Gatsby:导航链接到子页面上的顶级路由(使用 createPage 创建)不起作用