How to select where ID in Array Rails ActiveRecord without exception

前端 未结 6 800
余生分开走
余生分开走 2020-12-07 16:13

When I have array of ids, like

ids = [2,3,5]

and I perform

Comment.find(ids)

everything works fine. But w

6条回答
  •  既然无缘
    2020-12-07 17:01

    To avoid exceptions killing your app you should catch those exceptions and treat them the way you wish, defining the behavior for you app on those situations where the id is not found.

    begin
      current_user.comments.find(ids)
    rescue
      #do something in case of exception found
    end
    

    Here's more info on exceptions in ruby.

提交回复
热议问题