The user has a balance (user.balance).
The user can post bets on a game (bet.amount).
How could I stop the user from betting more than what is in their balance?
I assume I could create a validation that looks something like this?
def enough_funds?
@bet.bet_amount > self.current_user.balance
flash[:notice] = "You do not have the available funds for this bet"
end
I’m still new to rails, be gentle 🙂
You’re on the right track:
If
Bet‘s:bet_amountis less than the relatedUser‘s available:balance, the error will be added to:bet_amountattribute, invalidating the model instance.