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

  • SEARCH
  • Home
  • 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 6169979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:57:07+00:00 2026-05-23T22:57:07+00:00

I have two related validations on separate models. I can validate one way but

  • 0

I have two related validations on separate models. I can validate one way but not another. The post validation works: if I try to change the quantity available as the seller, and people have already ordered some of it, it (correctly) won’t let me change the quantity below the amount ordered already.

The order validation does not work: if I try changing the order amount to more than is available, it lets me. It checks only the current sum (I guess before the save), and does not notice the buyer trying to sneak out more than what’s available. When I try and change the order back to less than what is available, it won’t let me, checking the current sum ordered (before save).

I also tried validating the quantity of the order + the remaining available stock (available -sum ordered), but have the same problem.

How would I get the validation to check what the quantity would be after the save, and then not save it should the value be invalid? It would have to work also for editing an order

class Post < ActiveRecord::Base
    belongs_to :product
    belongs_to :event
    has_many :orders, :dependent => :destroy

    attr_accessible :quantity, :deadline, :product_id, :event_id

    validate :quantity_gt_ordered

    def quantity_gt_ordered
       self.errors.add(:quantity, " - People have already ordered more than this") unless self.quantity >= self.sum_orders
    end

    def sum_orders
        self.orders.sum(:quantity)
    end

class Order < ActiveRecord::Base
      belongs_to :user
      belongs_to :post

    attr_accessible :order_price, :post_id, :user_id, :quantity

    validate :quantity_is_available

    def quantity_is_available
       self.errors.add(:quantity, " - Please order only what's available") unless self.sum_post_orders <= self.post.quantity
    end

    def sum_post_orders
      Order.where(:post => self.post).sum(:quantity)
    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-23T22:57:07+00:00Added an answer on May 23, 2026 at 10:57 pm

    You should just be able to compare to the available quantity minus the ordered quantity:

    available_quantity = self.post.quantity - self.sum_post_order
    if quantity > available_quantity
      self.errors.add(:quantity, " - Please order only what's available")
    end
    

    And also make sure your sum_post_orders doesn’t include the current order:

    Order.where("id != ?", self.id).where(:post => self.post).sum(:quantity)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have two related tables in my database: Page and Tag. One Page can
I have two tables related: DataEntered and Model DataEntered -currentModel Model One DataEntered can
I have two related models (one to many) in my django app and When
I have two cmake-related problems: first, I can't make it to find the includes
I have two models, a MainModel and a related InlineModel that i'd like to
I have two related objects: ProgramSession and ProgramTask, with a one-to-many relationship. A ProgramSession
I have two related Python 'import' questions. They are easily testable, but I want
I have two related one to many entities Race and Cars (on race contains
I have one question related to Javascript Validation: How could I find the difference(the

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.