daily pastebin goal
81%
SHARE
TWEET

Untitled

a guest May 24th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def self.find_or_new(params = {}, opts = {})
  2.   opts[:id] ||= params[:id]
  3.   begin
  4.     find(opts[:id])
  5.   rescue ActiveRecord::RecordNotFound
  6.     Address.new(params)
  7.   end
  8. end
  9.  
  10. def self.messy_way(id = nil)
  11.   if id
  12.     begin
  13.       find(id)
  14.     rescue ActiveRecord::RecordNotFound
  15.       Address.new
  16.     end
  17.   else
  18.     Address.new
  19.   end
  20. end
  21.  
  22. def self.messy_way_refactor(id = nil)
  23.   if id
  24.     find_or_rescue(id)
  25.   else
  26.     Address.new
  27.   end
  28. end
  29.  
  30. def find_or_rescue(id)
  31.   begin
  32.     find(id)
  33.   rescue ActiveRecord::RecordNotFound
  34.     Address.new(params)
  35.   end
  36. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top