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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:19:11+00:00 2026-05-23T05:19:11+00:00

I have a model, Offer , that belongs to a User . Users have

  • 0

I have a model, Offer, that belongs to a User. Users have an attribute :prepaid_amount that represents an amount of money, and :bid_total, which is the sum of all money bid on items, multiplied by the quantities of each bid. So, a User with two bids, one with quantity => 3 and amount => 10, and another with quantity => 2 and amount => 3, has a bid_total of 36.

If the prepaid_amount is greater than this value, everything is fine. Assume the prepaid_amount is 40.

If I want to validate a new bid from the user, of quantity => 1, then the amount of the bid can be any integer above 0, and up to 4. If it’s zero, then 1*0, and the validation should fail because a bid must be greater than zero. If the amount is (say) 5, the bid should not validate, because 36 + 1*5 = 41, which is greater that the prepaid_amount.

How do I create such a validation in Rails 3 in the Offer model? Here’s what I have, which doesn’t work :

class Offer < ActiveRecord::Base

  belongs_to :user

  validates :user_id, :presence => true, :numericality => true do
    validates :qty, :presence => {:if => Proc.new { |offer| (offer.user.prepaid_amount-offer.user.bid_total)/offer.amount >= offer.qty} }
  end


end
  • 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-23T05:19:12+00:00Added an answer on May 23, 2026 at 5:19 am

    Have you considered using a custom validation method?

    validates_with :my_custom_validation_method
    
    def my_custom_validation_method
       ...your code to validate...
    end
    

    One might argue that this is not the perfect rails way of validation but if your validation is so complex you might consider isolating it in its own method.

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

Sidebar

Related Questions

I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
In my controller I have model operations that can return empty results. I've setup
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
I have a backbone view - which when called presents a form $('#add-offer').click(function() {
I have this strange error I don't understand. I have a model User defined
I have a Cruise offer class related to a model called SpecialInterest. I now
I have a House model in my rails app that has_many deals. I am
In a current project, I have a model called Box that stores items from
I have a model which is essentially just a string (django.db.models.CharField). There will only

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.