2017-03-17 39 views
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错误,而试图保存记录

回答