Topic: Validating against values in other records
I'm working on an auction service atm. Here's what I'm trying to accomplish:
- A bid must be higher than any previous bids on the same item.
- A bid must be higher than the min bid value set by the seller.
This pseudo-code is the best I came up with, but it's not working:
class Bid < ActiveRecord::Base
validates_exclusion_of :amount, :in => 0..Bid.find_all_by_item_id(bid.item_id).max
validates_exclusion_of :amount, :in => 0..Item.find(bid.item_id).min_bid
The main problem is I don't know how to refer to the bid-data (bid.item_id) from within the model.
If at all possible I'd like to do this validation in the model, but I'm suspecting I might need to move it to the controller?