If I have an Auction record, which has many Bids associated with it, out of the box I can do things like:
highest_bid = auction.bids.last(:all, :order => :amount)
But if I want to make this clearer (since it’s used in multiple areas in the code), where would I define the method:
highest_bid = auction.bids.highest_bid
Is this actually possible or do I have to drop down to looking it up from the Bid class directly?
highest_bid = Bid.highest_on(auction)
Sorry, I figured this out. I had tried adding the method to the ActiveRecord Bid class, but I’d forgotten to make it a class method so it wasn’t seeing the method.
Not 100% that this will handle the association however. Just writing some tests for this now.
EDIT:
A quick test seems to show that this seems to magically handle associations too.
The test would find the $50 bid if it wasn’t associating correctly. Voodoo 😉