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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:51:08+00:00 2026-05-30T00:51:08+00:00

I’ve got a process where user can activate a code that they’ve received: the

  • 0

I’ve got a process where user can activate a code that they’ve received: the activation process should do the following:

  1. Add X credits to the user account (User.credits)
  2. Create new creditlog item (just for history purposes)
  3. Mark the code as activated, so that user can not activate it again.

I’m having a specific controller for this task. I created a method “code_activate”, that works. But it’s not ACID compliant, I believe. Should this be a method in some model (user?), or in a controller as I’ve done it? How would you implement it “the correct way”?

Here’s the code:

  def code_activate
    code = CreditCodes.find_by_code(params[:code])
    unless code
      render :json => { :success=>false, :message=>(t :codedoesnotexist)}
      return
    end

    if code.activated
      render :json => { :success=>false, :message=>(t :codealreadyactivated)}
      return
    end

    # Add credits to user
    current_user.credits += code.amount
    current_user.save

    # Save credit log record
    cl = current_user.creditlog.new
    cl.actionid = 1
    cl.amount = code.amount
    cl.save

    # Set code as activated
    code.activated = true
    code.save

    # Show success message
    render :json => { :success=>true, :message=>((t :creditsadded).gsub(/@AMOUNT@/, code.amount.to_s)) }, :status => :ok
  end
  • 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-30T00:51:10+00:00Added an answer on May 30, 2026 at 12:51 am

    It’s business logic. It belongs into the model (CreditCode most likely)

    You want to put this in a transaction to ensure that adding the amount and marking the code happens all or nothing.

    Assume in your CreditCode you define a activate method like this:

    def activate(user)
      transaction do
        # Add credits to user
        user.credits += code.amount
        user.save
    
        # Save credit log record
        cl = user.creditlog.new
        cl.actionid = 1
        cl.amount = amount
        cl.save
    
        # Set code as activated
        activated = true
        save  
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.