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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:39:57+00:00 2026-05-28T06:39:57+00:00

I have something that should be really simple, but it’s killing me. l =

  • 0

I have something that should be really simple, but it’s killing me.

l = LineItem.first
#<LineItem id: 5, product_id: 1, quantity: 1, price: #<BigDecimal:7f7fdb51a3f8,'0.999E3',9(18)>, cart_id: 5, discount_percentage: 10, discount_amount: nil, discount_active: true, created_at: "2012-01-12 16:17:41", updated_at: "2012-01-12 16:17:41">

I have

l.discount_percentage.blank?
=> false

So, I have the following method:

  def total_price
    discount_amount = 0 if discount_amount.blank?
    discount_percentage = 0 if discount_percentage.blank?

    discounted_amount_from_percent = price*(discount_percentage.to_f/100)

    applicable_discount = [discount_amount,discounted_amount_from_percent].max

    return (price-applicable_discount)
  end

But when I do this:

l.total_price

Instead of returning 899, it returns 999 (meaning that the if discount_percentage.blank? didn’t work at all!)

Or the syntax WHATEVER_HERE if true/false only work in the View on Rails??

  • 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-28T06:39:58+00:00Added an answer on May 28, 2026 at 6:39 am

    Here lays the problem:

    discount_amount = 0 if discount_amount.blank?
    discount_percentage = 0 if discount_percentage.blank?
    

    Ruby “sees” variables from top to bottom and from left to right, so in that line he first sees a local variable (discount_amount =) so he decides this discount_amount thing in discount_mount.blank? is that same local variable (and not the instance method. You think the variable is not defined yet, but Ruby has already spotted it). Not having any value yet, discount_amountit set to default value nil, so nil.blank? succeeds and the assignment discount_percentage = 0 is made. Ditto for discount_percentage. Here’s a demo snippet:

    class ExampleClass 
      def run
        x = "it works as expected" if x == "x"
        x
      end
    
      def run2
        if x == "x"
          x = "it works as expected" 
        end
        x
      end
    
      def run3
        xy = "it works as expected" if x == "x"
        xy
      end
    
      def x; "x"; end
    end
    
    p ExampleClass.new.run #=> nil
    p ExampleClass.new.run2 #=> "it works as expected"
    p ExampleClass.new.run3 #=> "it works as expected"
    

    Step 1: don’t use the same names for local variables and instance methods. That’s usually a bad idea anyway because you lose track of which one you are using, but in this case it has really bitten you.

    Step 2: Do not write imperative code when you’re doing math calculations! Really, maths (9X % of the things you do in a typical application, (10-X)% being unavoidable side-effects) play well with expressions, not with statements. I’d write:

    def total_price
      final_discount_amount = discount_amount || 0
      final_discount_percentage = discount_percentage || 0
      discounted_amount_from_percent = price * (final_discount_percentage.to_f/100)
      applicable_discount = [final_discount_amount, discounted_amount_from_percent].max
      price - applicable_discount
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do something that should be really simple. I have created a
I am trying to do something that should be really simple. I want to
Really simple question: Am I missing something? Seems like this should be all that
I'm trying to do something that should be very simple, but it's causing this
This is probably something really simple that I am blind to see, but the
I'm having quite a bit of trouble with something that should be really simple.
I am struggling with something that I think should be easily (ish). I have
I am struggling on something that, I am sure, should be easy. I have
I have something here that is really catching me off guard. I have an
Ok this should be really simple, but I am doing my head in here

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.