1

次のシリアライザがあります

私の画像テーブルには、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
4

3 に答える 3

0

代わりにこれを試してくださいimage_id = Images.where(post_id: id).first

行う

image_id = Image.where(post_id: id).first

機種名書き間違えたImages

それは複数形です

レールが使用されている間

単数形

モデル名についてImage

注:idこの行の前に値を設定する必要があります

于 2016-07-27T13:46:23.483 に答える
0

交換

image_id.nil?

image_id.present? (As always to check object)
于 2016-07-27T13:55:57.590 に答える