Before I start littering my code with an unwieldy method of my own creation, I’m wondering if there’s an existing Ruby/Rails method that will:
- Returns true if @foo exists and @foo has an ID (e.g. you’re in the foo#show)
- Returns false if @foo exists but does not yet have an ID (e.g. you’re in foo#new)
- Returns false if @foo does not exist (e.g. you’re in bar#index)
I’ve tried to search the API docs but the terms I’m using are by necessity so general (ID, save, exists, etc) that I’ve not found anything so far.
You can use
.trywill return id or nil.
If you strictly want true or false,