Reputation: 4976
I have a Notifications
table that a query using an ID provided by the url parameters hash.
Say I have the following URL : videos/11?notification=5
The 5
maps to a notification. But say a user change 5
for 7
and submits the URL. If a notification with ID 7 does not exist, Rails sends an error saying it could not find a notification with ID 7
. I would like it to simply ignore the error and continue. I am making some tests after the find so nothing unwanted happens.
Upvotes: 1
Views: 104
Reputation: 5791
In your controller, you are likely using Notification.find
. This will throw an error if no record can be found with the given id. The finder method Notification.find_by_id
will not throw an exception if no record can be found, and will instead return nil
.
Upvotes: 3
Reputation: 7232
You can do this :
Notification.find_by_id 7
It will return nil if there is no records with id 7.
Upvotes: 2