briancolfer

briancolfer

Agile Web Development with Rails 7: Iteration E2 the add_product method

The add_product method assumes that the default value for the quantity attribute is 0 when it is nil.

I think the code example for add_product:

	​def​ ​add_product​(product)
​ 	  current_item = line_items.​find_by​(​product_id: ​product.​id​)
​ 	  ​if​ current_item
​ 	    current_item.​quantity​ += 1
​ 	  ​else​
​ 	    current_item = line_items.​build​(​product_id: ​product.​id​)
​ 	  ​end​
​ 	  current_item
​ 	​end​

Should be:

  def add_product(product)
​    ​# replace multiple items for a single product in a cart with a​
​    # single item​
    current_item = line_items.find_by(product_id: product.id)
    if current_item
      if current_item.quantity.nil?
        current_item.quantity = 1
      else
        current_item.quantity += 1
      end
    else
      current_item = line_items.build(product_id: product.id, quantity: 1)
    end
    current_item
  end
0 655 4

Marked As Solved

briancolfer

briancolfer

I forgot the default value in the migration.

Popular Prag Prog topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
10 2605 9
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 4020 2
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 1158 5
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 ...
0 4828 3
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
5 4327 7
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 1390 5
New
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
3 1944 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
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
0 2035 5
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
144 8219 50
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
105 4397 51
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
118 6884 31
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
199 3356 78
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
31 3355 16
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
21 10826 8
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
4 4593 1
New
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
62 6125 20
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
1 1089 1
New

Latest in PragProg

View all threads ❯