Можно использовать один оператор и много операторов выбора
update tbl
set title =
case
when title in ('a-1', 'a.1') then 'a1'
when title in ('b-1', 'b.1') then 'b1'
else title
end
, Конечно, это вызовет запись на каждой записи, и с индексами, это может быть проблема, таким образом, можно отфильтровать только строки, Вы хотите измениться:
update tbl
set title =
case
when title in ('a-1', 'a.1') then 'a1'
when title in ('b-1', 'b.1') then 'b1'
else title
end
where
title in ('a.1', 'b.1', 'a-1', 'b-1')
, Который сократит количество записей к таблице.
Метод def может служить оператором "begin":
def foo
...
rescue
...
end
You can also rescue inline:
1 + "str" rescue "EXCEPTION!"
will print out "EXCEPTION!" since 'String can't be coerced into Fixnum'
I'm using the def / rescue combination a lot with ActiveRecord validations:
def create
@person = Person.new(params[:person])
@person.save!
redirect_to @person
rescue ActiveRecord::RecordInvalid
render :action => :new
end
I think this is very lean code!