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

  • SEARCH
  • Home
  • 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 9170525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:02:07+00:00 2026-06-17T16:02:07+00:00

I’m not sure whether this is a problem of scopes or something, but I’m

  • 0

I’m not sure whether this is a problem of scopes or something, but I’m having problems with updating an object in a test:unit.

The interesting thing is that all functions work perfectly in development mode, but as soon as I switch to rake test:unit, it all breaks.

Here’s the relevant code:

  class Cart < ActiveRecord::Base

  def add_product(product)
    current_item = line_items.find_by_product_id(product.id)
    if current_item
      Rails::logger.debug "Incrementing quantity"
      Rails::logger.debug current_item.quantity
      current_item.quantity += 1
      Rails::logger.debug current_item.quantity
    else
      current_item = line_items.build(product_id: product.id,
                                     price: product.price)
    end
    Rails::logger.debug "Added Product"
    Rails::logger.debug current_item.quantity
    current_item
  end

and the associated test

  test "create new cart with one item added twice" do
    cart = Cart.new
    cart.add_product(products(:one)).save!
    assert_equal 1, cart.line_items.size
    assert_equal 36.00, cart.total_price
    Rails::logger.debug cart.line_items.to_a
    cart.add_product(products(:one)).save!
    Rails::logger.debug "Added second item"
    Rails::logger.debug cart.line_items.to_a
    Rails::logger.debug cart.total_price
    assert_equal 1, cart.line_items.size
    assert_equal 72.00, cart.total_price
  end

And here’s the log output:

Incrementing quantity
1
2
Added Product
2
   (0.1ms)  SAVEPOINT active_record_1
   (0.3ms)  UPDATE "line_items" SET "quantity" = 2, "updated_at" = '2013-01-18 15:27:06.958210' WHERE "line_items"."id" = 980190963
   (0.1ms)  RELEASE SAVEPOINT active_record_1
Added second item
[#<LineItem id: 980190963, product_id: 1, cart_id: nil, created_at: "2013-01-18 15:27:06", updated_at: "2013-01-18 15:27:06", quantity: 1, price: 36>]
36

ew quantity (which has changed according to the last log).
Interestingly, within the function itself, the quantity field is updating to two. It even saves to the SQL database. But when I call up cart.line_items, it even has the updated field, but it doesn’t save the new quantity (which has changed according to the last log).

  • 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-17T16:02:08+00:00Added an answer on June 17, 2026 at 4:02 pm

    Rails does not have an identity map by default. This means that whenever you do a LineItem.find(1) and another LineItem.find(1) you’ll get two separate objects that are created from the same row of data in your database but do not have any connection between them. If one of them is changed and saved to the database the other object won’t know about it and still have the old data.

    In your example you’re doing line_items.find_by_product_id(product.id) which will do a find and return a new object every time. There’s no connection to any LineItem objects previously loaded into cart.line_items.

    There are two options to update the data on stale objects. One of them is the .reload method which will reload all the data for one object from the database. The other option is passing a true to the cart.line_items association. A call like cart.line_items(true) will force a new query to the database to get all line items.

    The reason that this bug only hits you during test is simple: In the development mode, inserting and reading is usually done in two separate requests and therefore all the objects are freshly loaded from the database every time.

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

Sidebar

Related Questions

I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.