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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:19:24+00:00 2026-06-02T21:19:24+00:00

I had a quick question while going though the Agile Web Development with Rails

  • 0

I had a quick question while going though the Agile Web Development with Rails book that I wasn’t able to find the answer to while looking through the website. I have two methods:

This method is in my controller:

def decrement
  @line_item = LineItem.find(params[:id])
  @line_item = @line_item.decrement_quantity(@line_item.id)

  respond_to do |format|
    if @line_item.save
      format.html { redirect_to store_url }
      format.json { render json: @line_item, status: :created, location: @line_item }
    else
      format.html { render action: "new" }
      format.json { render json: @line_item.errors, status: :unprocessable_entity }
    end
  end

And this is in the corresponding model:

def decrement_quantity(line_item_id)
  current_item = LineItem.find_by_id(line_item_id)

  if current_item.quantity > 1
    current_item.quantity -= 1
  else
    current_item.destroy
  end

  current_item
end

I know this is not the most efficient code, but my question is if current_item gets destroyed in the model method, then what is that method returning? (nil?) Is current_item as a variable still existent, just the database object has been destroyed? How can the decrement method in the controller save an object that has been destroyed? (I put a logger.debut statement in the controller method’s if statement, and it seems that the code always goes through there whether or not the model method evaluated the if or else statement).

  • 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-06-02T21:19:26+00:00Added an answer on June 2, 2026 at 9:19 pm

    The model still exists for the duration of the call, but it has been removed from the database and if you call current_item.destroyed? then it will return true.

    Even if the item has been destroyed, the save method returns true.

    Here is some terminal output that hopefully helps.

    1.9.2p290 :001 > current_item = Deck.first
      Deck Load (0.1ms)  SELECT "decks".* FROM "decks" LIMIT 1
     => #<Deck id: 2, name: "Test Deck 1", description: "This is a te> #snip
    1.9.2p290 :002 > current_item.destroyed?
     => false 
    1.9.2p290 :003 > current_item.destroy
       (0.2ms)  begin transaction
      SQL (23.1ms)  DELETE FROM "decks" WHERE "decks"."id" = ?  [["id", 2]]
       (5.0ms)  commit transaction
     => #<Deck id: 2, name: "Test Deck 1", description: "This is a te> #snip
    1.9.2p290 :004 > current_item.destroyed?
     => true 
    1.9.2p290 :005 > current_item.save
       (0.2ms)  begin transaction
       (0.1ms)  commit transaction
     => true 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a quick question regarding RegEx... I have a string that looks something
I'm new to iOS development, just had a quick question. I'm creating an app
I had a quick question that's been bugging me. I'd like to place a
Hey everyone, I had a quick question that should be easy for someone who
I had a quick question about cookie security that I wanted to run by
I had a quick question. Is it possible to save a file without actually
I had a quick question about Django URL configuration, and I guess REGEX as
I just had a quick question about git - how backwards compatible are git
I had a quick question regarding the datacontractserializer. Maybe it's more of a stream
I just had a quick question regarding loops in Ruby. Is there a difference

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.