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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:38:23+00:00 2026-05-23T03:38:23+00:00

I have Transaction objects that are combined in a Cart object. Some Transaction objects

  • 0

I have Transaction objects that are combined in a Cart object. Some Transaction objects belong_to Products and others belong_to Services. I want to combine similar Transactions when they are added to the cart (ie. update an existing record with new price and quantity information).

Here’s what I currently have (untested):

def update_cart
  if current_cart.transactions.find(:conditions => [service_id = self.service_id])
    @utransaction = current_cart.transaction.find(:conditions => [service_id = self.service_id])
    @utransaction.price = @utransaction.price + self.price
    @utransaction.save
  elsif current_cart.transactions.find(:conditions => [product_id = self.product_id])
    @utransaction = current_cart.transactions.find(:conditions => [product_id = self.product_id])
    @utransaction.price = @utransaction.price + self.price
    @utransaction.quantity = @utransaction.quantity + self.quantity
    @utransaction.save
  else
    nil
  end
end

However, I feel that this is very bulky and there is probably a better way of doing it. So, before I go any further, is there any built-in or better ways of updating existing objects in this manner?

Thanks!

  • 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-23T03:38:24+00:00Added an answer on May 23, 2026 at 3:38 am

    Assuming your Transaction can belong_to a Product or belong_to a Service, then it sounds like a Polymorphic association is your best bet, and may make your existing problem much simpler.

    Check out the Railscast on Polymorphic Associations.

    I think you’ll like it and learn a lot.

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

Sidebar

Related Questions

Here is the scenario: I have an object called a Transaction that needs to
I have a transaction log file in CSV format that I want use to
I have some XML that I am trying to deserialize. The Kronos_WCF object deserializes
I have a Transactions object (as a part of a shopping cart) that belongs_to
I have a list of 10 data objects that I want to insert/update to
I have a User object which has a collection of Transaction objects under it.
I have a Data Access Object TransactionDao. When you call TransactionDao.Save(transaction) I would like
Consider that I have a transaction: BEGIN TRANSACTION DECLARE MONEY @amount SELECT Amount AS
I have two IDbCommand objects that are created from an NHibernate session, and they
I have an issue with creating a transaction. I get an error back that

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.