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 Pragmatic Bookshelf topics Top

jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |> handle_event() |> render() but the correc...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
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
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New

Latest in PragProg Customers

View all threads ❯