John
John

Reputation: 4006

How to fix Ruby on Rails obj != nil error

I'm trying to ensure that an object is not nil in Ruby on Rails and I'm getting this error:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

This is what the object looks like in the debugger (it is not nil):

enter image description here

How do I correctly check for nil in this case? All cases?

I've found some similar posts but not this specific case: Check for nil gives error, and Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id.

Upvotes: 0

Views: 237

Answers (1)

Rob Wagner
Rob Wagner

Reputation: 4421

The != comparator is using the id to check equality. So it is checking if ob1.id != ob2.id, or in your case map_set.id != nil.id

To check if an object is nil, you can simply check if the object has a value. if object or if !object.nil?

Upvotes: 1

Related Questions