There is controller
can_buy = nil
if game.spot_price == 0
return {json: {error: 1, error_message: "You can do it only once"}, status: 400} if game.spots.where(user_id: user.id).length > 0
can_buy = true
else
can_buy = UserService.have_enough_money?(user, game.spot_price)
end
if can_buy
ActiveRecord::Base.transaction do
# purchase goes here
end
end
And if user makes a few requests at the same moment and the spot_price is 0, he can buy few times, but he can do it only once. How to prevent it? PS: sorry for my english
Start a transaction earlier:
if game.spot_price.zero? && game.spots.where(user_id: user.id).length > 0
return json: {error: 1, error_message: "You can do it only once"},
status: 400
end
ActiveRecord::Base.transaction do
if game.spot_price.zero? || UserService.have_enough_money?(user, game.spot_price)
...
else
raise ActiveRecord::Rollback, "Not permitted!"
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.