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:

Where Next?

Popular Pragmatic Bookshelf topics Top

ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Latest in Agile Web Development with Rails 7

Agile Web Development with Rails 7 Portal

Sub Categories: