Reputation: 6259
I want to check if the entry with the id 2 exists in the settings table and if not i want that the user gets redirected to the setting_new_path. My code looks like this:
def index
if Setting.find(2).present?
@patients = Patient.all
@patients_drucken = Patient.drucken
@anrede = Setting.find(1).anrede
respond_to do |format|
format.html # index.html.erb
format.json { render json: @patients }
end
else
redirect_to new_setting_path
end
end
But somehow i get the error:
Couldn't find Setting with id=2
I dont get why this is displayed as an error! I mean in the case its not defined i worte , redirect to new_setting_path! What made i wrong?
Upvotes: 0
Views: 842
Reputation: 5111
It means you don't have any record with id = 2 in Settings table. Just check it once.
Upvotes: 0
Reputation: 7937
#find
will throws an exception if the record is not found.
You should use #where
and #any?
instead :
if Setting.where( id: 2 ).any?
Upvotes: 1