Skip to content

Instantly share code, notes, and snippets.

@sweatpantsninja
Created December 19, 2012 23:39
Show Gist options
  • Save sweatpantsninja/4341731 to your computer and use it in GitHub Desktop.
Save sweatpantsninja/4341731 to your computer and use it in GitHub Desktop.
belongs_to :order
belongs_to :item
validate :item_has_not_been_sent?
def user
self.order.user
end
def item_has_not_been_sent?
matching_order_items = user.order_items.where(:item_id => self.item_id).where(:size => self.size)
matching_order_items = matching_order_items.where{id != self.id} unless new_record?
if matching_order_items.exists?
errors.add(:item_id, "This item has already been sent to this user")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment