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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:02:19+00:00 2026-05-21T16:02:19+00:00

I have a Trans (transaction) class in my application that submits a transaction to

  • 0

I have a Trans (transaction) class in my application that submits a transaction to the server. When a transaction is submitted, I also want to do an update on a parent User class to update their cached “balance.” Here is relevant code:

# tran.rb
class Tran < ActiveRecord::Base
    belongs_to :submitting_user, :class_name => 'User'
end

And my controller:

#trans_controller.rb
def create
        @title = "Create Transaction"

        # Add the transaction from the client
        @tran = Tran.new(params[:tran])

        # Update the current user
        @tran.submitting_user_id = current_user.id

        # ERROR: This line is not persisted
        @tran.submitting_user.current_balance = 4;

        # Save the transaction
        if @tran.save
            flash[:success] = 'Transaction was successfully created.'
            redirect_to trans_path

I have a couple of problems:

  1. When I update the current_balance field on the user, that balance isn’t persisted on the user after the transaction is saved. I think maybe I need to use update_attributes?
  2. I am not even sure that the code should be a part of my controller – maybe it makes more sense in the before_save of my model?
  3. Will either of these make this transactional?
  • 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-21T16:02:19+00:00Added an answer on May 21, 2026 at 4:02 pm
    def create
      title = "Create Transaction"
      @tran = Tran.new(params[:tran])
      @tran.submitting_user_id = current_user.id
      # to make it "transactional" you should put it after @tran.save
      if @tran.save
        current_user.update_attribute :current_balance, 4
        ...
    

    And yes – it is better to put it into after_save callback

    class Tran < AR::Base
      after_save :update_user_balance
    
      private
      def update_user_balance
        submitting_user.update_attribute :current_balance, 4
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that I want passed via the linebreaks filter. {% trans
I have oracle backend for my application. The schema is designed such that I
I have the following database tables/EF objects public class Transaction { //some other properties
So I have a couple SQL commands that I basically want to make a
I have a model class Transaction(models.Model): sender = models.ForeignKey(MyBankAccount, unique=False, related_name=transactions_sent) receiver = models.ForeignKey(RecipientBankAccount,
I have User object with a list of groups that it belongs to: public
Right now i have code that initiates transactions on SQL Server using the intended
I have a class which does operations on the database, but I want to
I have 2 concurrent threads that at the same time enter a (Spring) transaction
I have written a transaction like: BEGIN TRAN UPDATE [Table1] SET [Name] = 'abcd'

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.