Skip to content

Instantly share code, notes, and snippets.

@szymon-przybyl
Created July 18, 2011 15:59
Show Gist options
  • Save szymon-przybyl/1089943 to your computer and use it in GitHub Desktop.
Save szymon-przybyl/1089943 to your computer and use it in GitHub Desktop.
order
class Order < ActiveRecord::Base
belongs_to :product
belongs_to :user
validates_presence_of :user, :product, :product_quantity
validates_numericality_of :product_quantity,
:only_integer => true, :greater_than => 0, :less_than_or_equal_to => 10000, :allow_blank => true
validate :enough_product_quantity,
# if does not work
:if => lambda { |o| (!o.product_quantity.nil?) & (!o.product.nil?) & (!o.product.quantity_left.nil?) }
def enough_product_quantity
if product_quantity > product.quantity_left
errors.add(:product_quantity, :not_enough_quantity_of_product)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment