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?

0 709 2

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

New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
4 983 0
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
1 4071 2
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. ...
9 1173 1
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
1 1160 5
New
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
5 1484 3
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
5 1406 5
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
0 1460 5
New
NaplesDave
I am using Android Studio Chipmunk | 2021.2.1 Patch 2 Build #AI-212.5712.43.2112.8815526, built on July 10, 2022 Runtime version: 11.0....
0 1403 2
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
212 15008 90
New
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
160 3807 49
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...
105 14440 47
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
37 2727 14
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
52 4894 22
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
0 2781 0
New
CommunityNews
Will Swifties’ war on AI fakes spark a deepfake porn reckoning?
0 5956 0
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
0 2183 2
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
15 1138 15
New

Latest in Agile Web Development with Rails 7

Agile Web Development with Rails 7 Portal

Sub Categories: