ruby on rails - Update if exists, else do nothing? -


I am trying to update a value of the model if only the model is present. If it does not happen, then I do nothing, the search returns only to renewal or question / answer, but I do not want to make it.

I know that I can do it with a simple one:

  found = model.find_b_id (id) if the end of the updated content is found   

However, I think it is a way of doing this without specifying any temporary local values ​​in a call or if you have to.

How do I write a rail, which will update the record of no noise errors, is not it present?

The latest Rail 3.x

You can use the method < Rails before calling update_attributes on the result of Code> find_by_id or where

Try back zero If the exception is not present, then there is no silent record. If the record is present, then it will be updated.

  found = model.fund_b_id () .try (: update_attributes, {key: value})    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -