# adding items to cart first attemp fail

**URL:** <https://discuss.rubyonrails.org/t/adding-items-to-cart-first-attemp-fail/62921>\
**Category:** rubyonrails-talk\
**Created:** [February 21, 2012, 10:32am UTC](https://discuss.rubyonrails.org/t/adding-items-to-cart-first-attemp-fail/62921 "2012-02-21T10:32:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [February 21, 2012, 10:32am UTC](https://discuss.rubyonrails.org/t/adding-items-to-cart-first-attemp-fail/62921/1 "2012-02-21T10:32:39Z")

</div>

hi im workiing on ruby on rails im just wondering if my add\_product is wrong because when i add my product in to my cart at first attemp it fail and give me a

nil cannot concien bigdecimal error

but clicking return from my browser and add item at the second attemp it worked fine and but didnt count my first attemp of adding my product this is my add\_product method

my model/cart.rb : has\_many :line\_items, :dependent =\> :destroy &nbsp;&nbsp;def add\_product(product\_id) &nbsp;&nbsp;&nbsp;&nbsp;current\_item = line\_items.where(:product\_id =\> product\_id).first &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if current\_item &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current\_item.quantity = current\_item.quantity.to\_i + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current\_item = LineItem.new(:product\_id=\>product\_id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line\_items \<\< current\_item &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current\_item &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;def total\_price &nbsp;&nbsp;&nbsp;Array.wrap(line\_items).sum { |item| item.total\_price } &nbsp;&nbsp;end

my cart/show.html.rb: \<div class="cart\_title" \>Your Cart\</div\> &nbsp;&nbsp;&nbsp;&nbsp;\<table\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% for item in @cart.line\_items %\> &nbsp;&nbsp;&nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= item.quantity %\>&times;\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= item.product.title %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td class="item\_price" \>\<%= number\_to\_currency(item.total\_price) %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr class="total\_line" \> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td colspan="2" \>Total\</td\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td class="total\_cell" \>\<%= number\_to\_currency(@cart.total\_price) %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\</tr\> &nbsp;&nbsp;&nbsp;&nbsp;\</table\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= button\_to 'Empty cart', @cart, :method =\> :delete, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:confirm =\> 'Are you sure?' %\>

my model/line\_item.rb def total\_price &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if product.price &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product.price = product.price \* quantity &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.to\_d &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;end

full trace:

app/models/line\_item.rb:14:in `*' app/models/line_item.rb:14:in `total\_price' app/views/carts/show.html.erb:7:in `block in _app_views_carts_show_html_erb ___389431939__ 618846538' activerecord (3.1.1) lib/active_record/associations/collection_proxy.rb:91:in `each' activerecord (3.1.1) lib/active\_record/associations/collection\_proxy.rb:91:in `method_missing' app/views/carts/show.html.erb:3:in `\_app\_views\_carts\_show\_html\_erb\_\_\_389431939\_\_618846538' actionpack (3.1.1) lib/action\_view/template.rb:144:in `block in render' activesupport (3.1.1) lib/active_support/notifications.rb:55:in `instrument' actionpack (3.1.1) lib/action\_view/template.rb:142:in `render' actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:40:in `block (2 levels) in render\_template' actionpack (3.1.1) lib/action\_view/renderer/abstract\_renderer.rb:33:in `block in instrument' activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument' activesupport (3.1.1) lib/active\_support/notifications/instrumenter.rb:21:in `instrument' activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument' actionpack (3.1.1) lib/action\_view/renderer/abstract\_renderer.rb:33:in `instrument' actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:39:in `block in render\_template' actionpack (3.1.1) lib/action\_view/renderer/template\_renderer.rb:47:in `render_with_layout' actionpack (3.1.1) lib/action_view/renderer/template_renderer.rb:38:in `render\_template' actionpack (3.1.1) lib/action\_view/renderer/template\_renderer.rb:12:in `block in render' actionpack (3.1.1) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap\_formats' actionpack (3.1.1) lib/action\_view/renderer/template\_renderer.rb:9:in `render' actionpack (3.1.1) lib/action_view/renderer/renderer.rb:36:in `render\_template' actionpack (3.1.1) lib/action\_view/renderer/renderer.rb:17:in `render' actionpack (3.1.1) lib/abstract_controller/rendering.rb:120:in `\_render\_template' actionpack (3.1.1) lib/action\_controller/metal/streaming.rb:250:in `_render_template' actionpack (3.1.1) lib/abstract_controller/rendering.rb:114:in `render\_to\_body' actionpack (3.1.1) lib/action\_controller/metal/renderers.rb:30:in `render_to_body' actionpack (3.1.1) lib/action_controller/metal/compatibility.rb:43:in `render\_to\_body' actionpack (3.1.1) lib/abstract\_controller/rendering.rb:99:in `render' actionpack (3.1.1) lib/action_controller/metal/rendering.rb:16:in `render' actionpack (3.1.1) lib/action\_controller/metal/instrumentation.rb:40:in `block (2 levels) in render' activesupport (3.1.1) lib/active_support/core_ext/benchmark.rb:5:in `block in ms' /home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime' activesupport (3.1.1) lib/active_support/core_ext/benchmark.rb:5:in `ms' actionpack (3.1.1) lib/action\_controller/metal/instrumentation.rb:40:in `block in render' actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:78:in `cleanup\_view\_runtime' activerecord (3.1.1) lib/active\_record/railties/controller\_runtime.rb:24:in `cleanup_view_runtime' actionpack (3.1.1) lib/action_controller/metal/instrumentation.rb:39:in `render' actionpack (3.1.1) lib/action\_controller/metal/implicit\_render.rb:10:in `default_render' actionpack (3.1.1) lib/action_controller/metal/mime_responds.rb:268:in `block in retrieve\_response\_from\_mimes' actionpack (3.1.1) lib/action\_controller/metal/mime\_responds.rb:195:in `call' actionpack (3.1.1) lib/action_controller/metal/mime_responds.rb:195:in `respond\_to' app/controllers/carts\_controller.rb:22:in `show' actionpack (3.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send\_action' actionpack (3.1.1) lib/abstract\_controller/base.rb:167:in `process_action' actionpack (3.1.1) lib/action_controller/metal/rendering.rb:10:in `process\_action' actionpack (3.1.1) lib/abstract\_controller/callbacks.rb:18:in `block in process_action' activesupport (3.1.1) lib/active_support/callbacks.rb:416:in `\_run\_\_371692982\_\_process\_action\_\_266900328\_\_callbacks' activesupport (3.1.1) lib/active\_support/callbacks.rb:386:in `_run_process_action_callbacks' activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run\_callbacks' actionpack (3.1.1) lib/abstract\_controller/callbacks.rb:17:in `process_action' actionpack (3.1.1) lib/action_controller/metal/rescue.rb:17:in `process\_action' actionpack (3.1.1) lib/action\_controller/metal/instrumentation.rb:30:in `block in process_action' activesupport (3.1.1) lib/active_support/notifications.rb:53:in `block in instrument' activesupport (3.1.1) lib/active\_support/notifications/instrumenter.rb:21:in `instrument' activesupport (3.1.1) lib/active_support/notifications.rb:53:in `instrument' actionpack (3.1.1) lib/action\_controller/metal/instrumentation.rb:29:in `process_action' actionpack (3.1.1) lib/action_controller/metal/params_wrapper.rb:201:in `process\_action' activerecord (3.1.1) lib/active\_record/railties/controller\_runtime.rb:18:in `process_action' actionpack (3.1.1) lib/abstract_controller/base.rb:121:in `process' actionpack (3.1.1) lib/abstract\_controller/rendering.rb:45:in `process' actionpack (3.1.1) lib/action_controller/metal.rb:193:in `dispatch' actionpack (3.1.1) lib/action\_controller/metal/rack\_delegation.rb:14:in `dispatch' actionpack (3.1.1) lib/action_controller/metal.rb:236:in `block in action' actionpack (3.1.1) lib/action\_dispatch/routing/route\_set.rb:65:in `call' actionpack (3.1.1) lib/action_dispatch/routing/route_set.rb:65:in `dispatch' actionpack (3.1.1) lib/action\_dispatch/routing/route\_set.rb:29:in `call' rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call' rack-mount (0.8.3) lib/rack/mount/code\_generation.rb:96:in `block in recognize' rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `optimized\_each' rack-mount (0.8.3) lib/rack/mount/code\_generation.rb:95:in `recognize' rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call' actionpack (3.1.1) lib/action\_dispatch/routing/route\_set.rb:532:in `call' actionpack (3.1.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' rack (1.3.6) lib/rack/etag.rb:23:in `call' rack (1.3.6) lib/rack/conditionalget.rb:25:in `call' actionpack (3.1.1) lib/action\_dispatch/middleware/head.rb:14:in `call' actionpack (3.1.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call' actionpack (3.1.1) lib/action\_dispatch/middleware/flash.rb:243:in `call' rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context' rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call' actionpack (3.1.1) lib/action_dispatch/middleware/cookies.rb:331:in `call' activerecord (3.1.1) lib/active\_record/query\_cache.rb:62:in `call' activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call' actionpack (3.1.1) lib/action\_dispatch/middleware/callbacks.rb:29:in `block in call' activesupport (3.1.1) lib/active_support/callbacks.rb:392:in `\_run\_call\_callbacks' activesupport (3.1.1) lib/active\_support/callbacks.rb:81:in `run_callbacks' actionpack (3.1.1) lib/action_dispatch/middleware/callbacks.rb:28:in `call' actionpack (3.1.1) lib/action\_dispatch/middleware/reloader.rb:68:in `call' rack (1.3.6) lib/rack/sendfile.rb:101:in `call' actionpack (3.1.1) lib/action\_dispatch/middleware/remote\_ip.rb:48:in `call' actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call' railties (3.1.1) lib/rails/rack/logger.rb:13:in `call' rack (1.3.6) lib/rack/methodoverride.rb:24:in `call' rack (1.3.6) lib/rack/runtime.rb:17:in `call' activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.3.6) lib/rack/lock.rb:15:in `call' actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call' railties (3.1.1) lib/rails/engine.rb:456:in `call' railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call' railties (3.1.1) lib/rails/rack/log\_tailer.rb:14:in `call' rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service' /home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' /home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' /home/led/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start\_thread'

im having a problem in adding my first item in to my cart it fails but when adding second time it worked fine im just wondering if my adding product method is right? thanks in advance more power to us!
