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 548173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:03:04+00:00 2026-05-13T11:03:04+00:00

I’m running the following method and I’m successfully passing two arguments (inventory, quantity) into

  • 0

I’m running the following method and I’m successfully passing two arguments (inventory, quantity) into the method. However I’m incorrectly using .first and .each methods. I’m attempting to replace .each with the .select to select for the cart item with the Inventory id: 6

possible .each replacement: (does not function) inventory_to_increment = @items.select{|item| item_id == inventory}

def increment_inventory_quantity(inventory, quantity)
   inventory_to_increment = @items.each{|item| item.inventory == inventory}
   unless inventory_to_increment.empty?
      inventory_to_increment = inventory_to_increment.first
   else
      # error handling here
   end
   inventory_to_increment.quantity = quantity.to_i
end

I’ve used Ruby Debugger, to debug my code:

inventory_to_increment = @items.each{|item| item.inventory == inventory}

p inventory = 6

unless inventory_to_increment.empty?

CartItem:0x102c4a4c0 @quantity=22, @inventory=#<Inventory id: 1

CartItem:0x102c49638 @quantity=2, @inventory=#<Inventory id: 8

CartItem:0x102c48918 @quantity=4, @inventory=#<Inventory id: 50

CartItem:0x102c47b80 @quantity=2, @inventory=#<Inventory id: 6

inventory_to_increment.first

CartItem:0x102c4a4c0 @quantity=22, @inventory=#<Inventory id: 1 

inventory_to_increment.quantity = quantity.to_i

= 3

I’ve tried several combinations and I need some basic Ruby array guidance. Thank you in advance!

UPDATE OUTPUT FOR ANSWER

inventory_to_increment = @items.select{|item| item.inventory_id == inventory}
(rdb:1) list
[21, 30] in /Users/justin/cart/app/models/cart.rb
   21   
   22  
   23  def increment_inventory_quantity(inventory, quantity)
   24  debugger
   25  
=> 26     inventory_to_increment = @items.select{|item| item.inventory_id == inventory}
   27   unless inventory_to_increment.empty?
   28       inventory_to_increment.first
   29   end
   30  
(rdb:1) p @items
[#<CartItem:0x102df1828 @quantity=22, @inventory=#<Inventory id: 1>>, #<CartItem:0x102df09a0 @quantity=2, @inventory=#<Inventory id: 8>>, #<CartItem:0x102ded908 @quantity=21, @inventory=#<Inventory id: 50>>]
(rdb:1) p inventory
50
(rdb:1) p quantity
"11"
(rdb:1) p item.inventory.id
NameError Exception: undefined local variable or method `item' for #<Cart:0x102df18f0>
(rdb:1) p item.inventory_id
NameError Exception: undefined local variable or method `item' for #<Cart:0x102df18f0>
(rdb:1) next
/Users/justin/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:162
rescue_action(exception)
  • 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-05-13T11:03:05+00:00Added an answer on May 13, 2026 at 11:03 am

    It seems Inventory is an object and if so it’s likely that your item.inventory == inventory will not work as expected. Let’s say you have an Item class.

    class Item
        attr_accessor :inventory_id, :quantity
    
      def initialize(inventory_id, quantity)
        @inventory_id = inventory_id
        @quantity = quantity
      end
    end
    

    and then you create two objects and compare them:

    >> item1=Item.new(8,2)
    => #<Item:0xb7b733bc @inventory_id=8, @quantity=2>
    >> item2=Item.new(8,2)
    => #<Item:0xb7b6b7c0 @inventory_id=8, @quantity=2>
    >> item1==item2
    => false
    

    This is because you will compare the object ids (0xb7b733bc to 0xb7b6b7c0) instead of the content of the objects.

    >> item1.inventory_id==item2.inventory_id
    => true
    

    It then seems you only want to add to the quantity to the first item matching the inventory id. If that is correct then you can try something like this

     inventory_to_increment=items.select {|item| item.inventory.inventory_id==inventory}
     unless inventory_to_increment.empty?
        inventory_to_increment.first.quantity+=quantity
     end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
this is what i have right now Drawing an RSS feed into the php,

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.