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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:00:48+00:00 2026-05-13T00:00:48+00:00

Unfortunately this mess works: Do you have suggestings for cleaning up this code: I’m

  • 0

Unfortunately this mess works: Do you have suggestings for cleaning up this code: I’m trying to use a activerecord to compare two table columns, “needed” and “amount” then update a boolean column, depending on the returned data. Totally breaking do-not-repeat coding.

def update
    @inventory = Inventory.find(params[:id])

    respond_to do |format|
      if @inventory.update_attributes(params[:inventory])
            unless @inventory.needed.nil?
                if @inventory.needed < @inventory.amount then
                @inventory.instock = true
                @inventory.update_attributes(params[:inventory])
                else
                @inventory.instock = false
                @inventory.update_attributes(params[:inventory])
                end
            end
        flash[:notice] = 'Inventory was successfully updated.'
        format.html { redirect_to(@inventory) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @inventory.errors, :status => :unprocessable_entity }
      end
    end
  end

Cheers!

  • 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-13T00:00:48+00:00Added an answer on May 13, 2026 at 12:00 am

    First, keep the extraneous logic out of your controller:

    def update
      @inventory = Inventory.find(params[:id])
    
      respond_to do |format|
        if @inventory.update_attributes(params[:inventory])
          flash[:notice] = 'Inventory was successfully updated.'
          format.html { redirect_to(@inventory) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @inventory.errors, :status => :unprocessable_entity }
        end
      end
    end
    

    Then, handle the instock attribute with a callback on the Inventory model. Something like:

    before_update :check_instock, :unless => Proc.new { |inventory| inventory.needed.nil? } 
    
    def check_instock
      if needed < amount
        instock = true
      else
        instock = false
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to combine two images into a single image. unfortunately this has to
I have a one class to one table mapping; unfortunately this table has 110+
I'm trying to read the Prototype source. I've come to this part.(Unfortunately, this snippet
This is a bit of a specific request unfortunately. I have a CMS that
I've have searched on this and it seems to be a catch all, unfortunately
I have some code like this to take over the space bar's function: $(document).keypress(function
I found this thread while trying to resolve my issue unfortunately this I can't
I just want to understand how capturing and bubbling work. Unfortunately this code just
I am trying to input a path using optparser in python. Unfortunately this piece
Assembly.Location gives a plain path to the assembly. Unfortunately this is empty when running

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.