title: rails 4 Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
body: <p>I have the following serializer </p>
  
  <p>In my image table i have the data with id - 1,2,3,4</p>
  
  <p>If i pass id in my serializer as 5 instead of throwing empty result it is throwing exception as</p>
  
  <pre><code>Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
  </code></pre>
  
  <p>Why does this occurs and how can i resolve it.</p>
  
  <pre><code>def image_ids
    image_id = Images.where(post_id: id).first
    unless image_id.nil?
      image_id = image_id.id
      [image_id]
    end
  end
  </code></pre>