Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:47:39+00:00 2026-06-09T17:47:39+00:00

Via such book: Agile Web Development with Rails i’m creating shopping cart for my

  • 0

Via such book:

Agile Web Development with Rails

i’m creating shopping cart for my application. There is such code:

class Cart < ActiveRecord::Base
    attr_accessible :id
    has_many :line_items, dependent: :destroy

  def add_article(article_id)
    current_item = line_items.find_by_ART_ID(article_id)
      if current_item
      current_item.quantity += 1
      else
      current_item = line_items.build(ART_ID: article_id)
      end
    current_item
  end
  def total_price
    line_items.to_a.sum { |item| item.total_price(item.ART_ID) }
  end
  def total_count
    line_items.to_a.sum { |item| item.quantity }
  end

end

On my previous project on rails 3.0.9 all was ok, but now it’s say that

nil can’t be coerced into Fixnum
in db, quantity is null

if i change my code for like this

current_item = line_items.find_by_ART_ID(article_id)
          if current_item
          current_item.quantity = 1
          else
          current_item = line_items.build(ART_ID: article_id)
          current_item.quntity = 1
          end
        current_item

all is good, but what’s wrong? why rails 3.2.6 and ruby 1.9.3 don’t understand my += assignments?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:47:41+00:00Added an answer on June 9, 2026 at 5:47 pm

    Its not a problem with += , the problem is you are try to do this with your code.

    nil + 1
    

    quantity is nil for the first time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I learn bindings in WPF via book. I have wrote such code: using System;
It seems to me that Rails shines when building web-centric applications such as the
I have a Rails application running on a remote Linux desktop at work via
Is it possible to post binary files (such as images) to GitHub gists via
Maybe you all know that .app bundle is easily accessible for reading via such
I understood how I change CSS via media queries (such as media=screen and (max-width:640px))
so I am able to successfully have another class of mine be notified via
I'm producing a projection via: var query = from book in books select new
I am trying to understand when should you test via BDD such as cucumber/specflow,
I am working on GNOME's Documents application for my Google Summer of Code project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.