ruby-on-rails - 非标准键引用的导入任务
问题描述
这样的两个类的关联,
class Available < ActiveRecord::Base
belongs_to :structure, class_name: 'Structure', foreign_key: 'product_code'
class Structure < ActiveRecord::Base
self.primary_key = 'product_code'
在 rake 任务下导入时会产生后果
begin
Structure.create(
product_code: row[0],
destination: row[1],
创建以 id 作为 product_code 的记录,以及 product_code 属性本身 #<Structure id: "CAN752500", product_code: "CAN752500", destination: [...]
我注意到,在未在Structure
模型上定义关联的原始导入中,属性id
具有其预期值;在模型中指定后self.primary_key
,数据库中的值id
被该product_code
值覆盖。
更新的问题
这个期望的目标是,保持structure.id
为整数,同时具有product_code
功能上的关联。
如何定义?
解决方案
推荐阅读
- node.js - 离子和电子应用程序未启动。启动应用程序时出错,在 {Project Directory} 中找不到模块
- maven - 从基于 Restlet 的 Maven 应用程序生成 Swagger 文档
- python - Python stop for loop until certain string exists in a continously changing log file
- php - 从自定义字段值替换 Woocommerce 产品可变价格范围
- powerapps - 如何在 Powerapps 中动态更改库源?
- flutter - Flutter debug Apk反编译获取源码
- angularjs - 什么会导致 AngularJS 中的 ng-class 附加类,而不是删除它?
- c# - LLBLGen Pro-加入与预取路径
- ios - 如何修复 Xcode 中的“没有从 LeakAgent 请求收到回复字典”错误?
- python-3.x - Pandas 在比较相同的数据帧时“只能比较标记相同的 DataFrame 对象”。