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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:12:49+00:00 2026-06-18T01:12:49+00:00

I have a CommercialDocument model which have a discount_amount attribute and a discount_amount_with_tax virtual

  • 0

I have a CommercialDocument model which have a discount_amount attribute and a discount_amount_with_tax virtual attribute.

Here is how I defined this in my model :

def discount_amount_with_tax
  discount_amount * (1 + tax.rate / 100)
end

def discount_amount_with_tax=(amount)
  self.discount_amount = amount.to_f / (1 + tax.rate / 100)
end

In my form, a user can fill in both discount_amount and discount_amount_tax :

= f.label :discount_amount
= f.text_field :discount_amount
= f.text_field :discount_amount_with_tax

I want to give the priority to the discount_amount_with_tax field, which means that discount_amount must not be taken into account unless the other field is empty.

My problem is that if I put nothing in the discount_amount_with_tax field, and let’s say 10 in discount_amount, then discount_amount will be equal to 0, which is clearly not what I want.

How can I fix this ?

Any help would be greatly appreciated.

  • 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-18T01:12:50+00:00Added an answer on June 18, 2026 at 1:12 am
    "".to_i
    # => 0
    

    A blank string converts to a zero integer. Therefore:

    def discount_amount_with_tax=(amount)
      self.discount_amount = amount.to_f / (1 + tax.rate / 100)
    end
    
    # same as...
    
    def discount_amount_with_tax=(0)
      self.discount_amount = 0 / (...)
    end
    
    # 0 / anything except zero = 0
    # self.discount_amount = 0 no matter what
    

    During mass-assignment, discount_amount_with_tax= is called. A blank form input is passed as an empty string, which Active Record then converts to an integer (zero). discount_amount_with_tax= sets discount_amount to zero regardless of discount_amount‘s previous value.

    Easy way around this is to use a conditional:

    def discount_amount_with_tax=(amount)
      self.discount_amount = (amount.to_f / (1 + tax.rate / 100)) if amount > 0
    end
    

    Mind you, this is the easy way, not the ideal way. A better solution is to write custom setter logic in the controller in lieu of mass-assignment; basically to manually set these attributes in the controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
have defined a Java Interface with the annotation @WebService Compiled the code which went
have this code import threading def Thread(f): def decorator(*args,**kargs): print(args) thread = threading.Thread(target=f, args=args)
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have an issue with marshall and unmarshall readers and writers. So here it is.
Have got a method which returns IEnumerable<User> which I have been using Linq /
Have been trying to figure this problem out for a while now and was
have Intel Core i7 laptop with 8 GB ram. This is my first time

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.