Reputation: 4213
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:
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.
Upvotes: 3
Views: 993
Reputation: 167
For first 2, use [email protected]_record?
new_record? method returns true/false if model was saved to database and have id or not.
For third, read this: defined?
Upvotes: 0
Reputation: 222428
You can use .try
@foo.try(:id)
will return id or nil.
If you strictly want true or false,
[email protected](:id)
Upvotes: 4