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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:15:31+00:00 2026-05-29T10:15:31+00:00

I have an e-commerce application that is exhibiting strange behavior. The product is a

  • 0

I have an e-commerce application that is exhibiting strange behavior. The product is a training class. The student creates reservations for various classes, which are added as line-items to a cart. At some point, and order is created and paid for, at which point I need to iterate through each of the reservations, and update the “paid” attribute. So here is the database construction…

1) Class has_many Reservations
2) Reservation belongs_to class, and has_one line_item (and has a decimal :paid attribute)
3) Line_item belongs_to reservation, and belongs_to cart
4) Cart has_many line_items, and has_one order

So, when the student creates the order, a credit card transaction is performed, and if successful, I want to generate and email a receipt to the student.

Originally, in my OrdersController#create method looked something like this…

def create
  @cart = current_cart #retrieves the cart 
  @order = @cart.build_order(params[:order])
  # snip saving order and purchase transaction
  # I would then iterate over the line-items with
  @cart.line_items.each { |li| li.reservation.update_attribute(:paid, li.reservation.fee)}

At this point, however I discovered that the value of the paid attribute would depend on how the line_item was accessed. If I accessed via…

@cart.line_items.each {|li| li.reservation.paid }

the values would be as set. HOWEVER, If it used…

@order.cart.line_items.each {|li| li.reservation.paid }

The value would be zero. (But it was accessing the proper reservation.id)

Then I discovered that if I changed the line that set the paid attribute to the following…

  @order.cart.line_items.each { |li| li.reservation.update_attribute(:paid, li.reservation.fee)}

That either of the accesses above would return the correct value.

So I guess my question is, what is the difference in setting the :paid attribute with

@cart.line_items.each ...

and

@order.cart.line_times.each ...

Based on the comment below, I’ve moved the iteration into the Cart model. it now has

def paid_deposit!
  self.line_items.includes(:reservation).each do |li|
    li.reservation.update_attribute(:paid, li.reservation.deposit)
  end
end

However, the same issue exists
The controller has the following code…

@cart.paid_deposit!
@cart.line_items.each {|li| logger.debug "Cart: Res: #{li.reservation.id}, Paid: #{li.reservation.paid}" }
@order.cart.line_items.each {|li| logger.debug "Order: Res: #{li.reservation.id}, Paid: #{li.reservation.paid}" }

The resulting log is…

Reservation Load (0.3ms)  SELECT `reservations`.* FROM `reservations` WHERE `reservations`.`id` IN (346, 347, 348)
(0.2ms)  BEGIN
(0.2ms)  UPDATE `reservations` SET `paid` = 40.0, `updated_at` = '2012-02-05 22:12:36' WHERE `reservations`.`id` = 346
(0.4ms)  COMMIT
(0.1ms)  BEGIN
(0.1ms)  UPDATE `reservations` SET `paid` = 40.0, `updated_at` = '2012-02-05 22:12:36' WHERE `reservations`.`id` = 347
(0.6ms)  COMMIT
(0.1ms)  BEGIN
(0.1ms)  UPDATE `reservations` SET `paid` = 25.0, `updated_at` = '2012-02-05 22:12:36' WHERE `reservations`.`id` = 348
(0.3ms)  COMMIT
LineItem Load (0.1ms)  SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`cart_id` = 24
Reservation Load (0.2ms)  SELECT `reservations`.* FROM `reservations` WHERE `reservations`.`id` = 346 LIMIT 1
Cart: Res: 346, Paid: 40.0
Reservation Load (0.1ms)  SELECT `reservations`.* FROM `reservations` WHERE `reservations`.`id` = 347 LIMIT 1
Cart: Res: 347, Paid: 40.0
Reservation Load (0.1ms)  SELECT `reservations`.* FROM `reservations` WHERE `reservations`.`id` = 348 LIMIT 1
Cart: Res: 348, Paid: 25.0
Order: Res: 346, Paid: 0.0
Order: Res: 347, Paid: 0.0
Order: Res: 348, Paid: 0.0

As can be seen, referencing the paid amount from the cart gets the proper values, doing the same from the Order does not. Apparently using cached values? Its not accessing the database.

  • 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-29T10:15:32+00:00Added an answer on May 29, 2026 at 10:15 am

    This is kind of an awful way to go about updating things like this. I hope you have a good unit or functional test to demonstrate this broken behaviour so you can be sure it’s fixed when you get it right.

    You should probably make a model method to help facilitate this sort of thing. Doing a lot of heavy lifting in your controller makes it hard to test as you can’t easily access the controller environment from the interactive console.

    What I can’t figure out is how calling an accessor method like paid does anything useful in your each loop. That should return a number, not set anything. The update_attribute version should work because it’s actually changing data.

    What you could do is push this into the model and make a method that does something like this:

    class Cart < ActiveRecord::Base
      def paid!
        self.line_items.include(:reservation).each do |li|
          li.update_attribute(:paid, li.reservation.fee)
        end
      end
    end
    

    If you can do this all in-database, this might be faster:

    class Cart < ActiveRecord::Base
      def paid!
        self.line_items.include(:reservation).update_all(
          'paid=reservations.fee'
        )
      end
    end
    

    Usually I look for ways to side-step the model if just twiddling fields that aren’t subject to validation checks.

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

Sidebar

Related Questions

I have an asp.net mvc e-commerce application that requires voucher payment. how to pass
In our e-commerce application, we have different kinds of products having different kinds of
I have some e-commerce code that I use often that uses Linq To SQL
I have an ASP.NET 3.5 e-commerce site that has an admin section. I want
I'm building an e-commerce website. I have a Product and Order models. It's possible
We're in the middle of developing a e-commerce application that will be used by
I am developing a stock/e-commerce web application. We have been using a version of
We have a database for an e-commerce application, we are working with JPA 2.0.
We have an E-Commerce site that allows users to buy products, view their shopping
I have been trying to work with DDD style for my e-commerce application. Most

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.