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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:49:05+00:00 2026-06-09T02:49:05+00:00

I have a Price model with 4 different fields: t.decimal amount t.decimal amount_per_unit t.decimal

  • 0

I have a Price model with 4 different fields:

t.decimal  "amount"
t.decimal  "amount_per_unit"
t.decimal  "unit_quantity"
t.string   "unit"

I’m trying to make a custom validation that allows either the amount or amount_per_unit fields (this includes unit quantity and unit) to be filled but not both of them. So to make a word diagram of what I mean.

amount = YES
amount_per_unit + unit + unit_quantity = YES

amount_per_unit (alone or amount.present) = NO
unit_quantity (alone or amount.present) = NO
unit (alone or amount.present) = NO
amount and amount_per_unit + unit + unit_quantity = NO

if you still are confused, just know that its either the amount itself thats filled in or the amount per unit fields that are (1 or 3).

So far I tried this validation in my Price model:

validates :amount, :numericality => true                                
validates :amount_per_unit, :numericality => true       
validates :unit_quantity, :numericality => true
validates :unit, :inclusion => UNITS

validate :must_be_base_cost_or_cost_per_unit

private

  def must_be_base_cost_or_cost_per_unit
    if self.amount.blank? and self.amount_per_unit.blank? and self.unit.blank? and self.unit_quantity
        #   one at least must be filled in, add a custom error message
        errors.add(:amount, "The product must have a base price or a cost per unit.")
        return false
    elsif !self.amount.blank? and !self.amount_per_unit.blank? and !self.unit.blank? and !self.unit_quantity
        #   both can't be filled in, add custom error message
        errors.add(:amount, "Cannot have both a base price and a cost per unit.")
        return false
    else
        return true
    end
  end

This validation doesn’t work though as all fields are blank it results to a numericality error and if I fill all of them, it creates the price with all fields filled. What needs to be fixed?

  • 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-09T02:49:07+00:00Added an answer on June 9, 2026 at 2:49 am

    I think your values are coming in as nil, not blank.

    Try changing the second condition to:

    elsif !self.amount.to_s.blank? and !self.amount_per_unit.to_s.blank? and !self.unit.to_s.blank? and !self.unit_quantity.to_s.blank?
    

    Also, it seems you have a typo on the last condition on both statements (e.g. !self.unit_quantity instead of !self.unit_quantity.to_s.blank?

    I hope that helps.

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

Sidebar

Related Questions

I have 3 models. These are the User , Product and Price model. I
I have an existing Rails 3.1 application with a model Products with price, description,
I have the following model associations: class Product < ActiveRecord::Base has_many :prices class Price
I have a standard devise user model with the usual fields. This is for
I have a db table with 3 different {price (value> int)} parameters I would
I have a different price set for products based on customer group. I want
I have a 'price' variable that contains some integer number from a MySQL database.
I have generated inputs with price values. Example: <input type=text value=59,00/> Now I should
I have a free shipping price rule which is configured like this: All customer
I have a class level price variable decalred inside a page, like this: public

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.