我有以下的串行轨道4名为id的零,这将被错误地4 - 如果你真的想零的ID,请使用OBJECT_ID
在我的形象表我有ID的数据 - 1, 2,3,4
如果我通过ID在我的串行5而不是抛出空的结果是抛出异常,因为
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
为什么会发生这种情况,我该如何解决这个问题。
def image_ids
image_id = Images.where(post_id: id).first
unless image_id.nil?
image_id = image_id.id
[image_id]
end
end
'Images.where(POST_ID:ID).first'是零,这就是所有 – apneadiving
@apneadiving对不起,我不能让你.. – overflow
他的意思是你的数据库查询没有产生任何结果。你无法得到任何东西的ID。您首先需要检查查询是否有任何结果,然后您可以返回该ID,否则返回任何(无) – KappaNossi