0
我想手動將值設置爲我的表中不是主鍵的字段「ID」。每次我用.ID屬性和值分配給它,它就會被分配到主鍵(abc_id),而不是「ID」Rails:如何將值設置爲不是主鍵的「id」列
table/model: MyModel
primary key: abc_id
another column: id
我嘗試以下保存的ID,但他們沒有辦法似乎工作。
test = MyModel.new
test.id = 555
==> updates abc_id
test.write_attribute(:id, 555)
==> private method not available
test.update_attribute(:id,555)
==> updates abc_id -->ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint
test.attributes = {id: 555}
==> no error but doesn't do anything
test.send(:attributes=, id: 555)
==> saves other attributes but not id
=> nil
我怎麼能值分配給「ID」,因爲我總是得到PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint
錯誤,而試圖保存記錄