Called id for nil, which would mistakenly be 4 错误解决之一_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > Called id for nil, which would mistakenly be 4 错误解决之一

Called id for nil, which would mistakenly be 4 错误解决之一

 2010/12/26 9:47:23  qwlong  http://qwlong.javaeye.com  我要评论(0)
  • 摘要:Calledidfornil,whichwouldmistakenlybe4--ifyoureallywantedtheidofnil,useobject_id这种问题如果是在一个事务中,你使用Customer.new的话,然后你又对customer进行一系列的操作,如下示例:然后当你在save的时候就会报以上的错误customer=Customer.new(attr)customer.channel_type="101"customer.channel_id=101customer
  • 标签:for 解决 all 误解 错误
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
这种问题如果是在一个事务中,你使用Customer.new的话,然后你又对customer进行一系列的操作,如下示例:
然后当你在save的时候就会报以上的错误
customer = Customer.new(attr)
      customer.channel_type = "101"
      customer.channel_id = 101
      customer.sales_id = 101
customer.save! #此处会报错

此时的解决办法就是把new 改为 create然后就可以了

customer = Customer.create(attr)
      customer.channel_type = "101"
      customer.channel_id = 101
      customer.sales_id = 101
  customer.save!
  如此代码为正确

具体原因尚未了解,希望高人解答!
发表评论
用户名: 匿名