s2k

s2k

Author of Fast Feedback Using Ruby

Agile Web Development with Rails 7: Getting or creating a cart (page 114)

Hi all,

the code on page 117 to get the of create a cart object is:

def set_cart
  @cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound 
  @cart = Cart.create 
  session[:cart_id] = @cart.id
end

Since I’d expect to not find a cart, I think it’s not very exceptional to not find a cart.
Therefore, I would probably tend to not using an exception to control the program flow and use code like this instead:

def set_cart
  @cart = Cart.find_by(id: session[:cart_id])
  return unless @cart.nil?

  @cart = Cart.new
  session[:cart_id] = @cart.id
end

Or maybe this:

def set_cart
  @cart = Cart.find_by(id: session[:cart_id])
  if @cart.nil?
    @cart = Cart.new
    session[:cart_id] = @cart.id
  end
end

I must say that I also do like the

def …
  # …
rescue … 
  # … 
end

notation (a lot), provided that the rescued behaviour is in fact exceptional.
Am I wrong? What are other people thinking about this?

Marked As Solved

s2k

s2k

Author of Fast Feedback Using Ruby

Thanks for the link.
I wasn’t aware that the topic hasn’t been settled and agreed upon in the past … 10 years.
Or maybe people agreed to disagree on this point.

In any case, following Dave Thomas’ original choice can’t be too bad. :slightly_smiling_face:

Popular Prag Prog topics Top

New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
PragmaticBookshelf
Author Spotlight Erin Dees @undees Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New