I want to use columns (numerical) that continue to change in rails for validation.

Asked 1 years ago, Updated 1 years ago, 121 views

In rails, we have created a function to use (pay) points (quantities).
I would like to validate the amount of payment.

Specifically, if the payment amount is below the account balance of the payment user, it will be considered a successful validation, and I would like to update the balance of the BankAccount model.
(If the balance is 1000, the payment amount is 1000, the model will be updated. If the payment amount is 1001 or more and the balance is negative, the model will not be updated.)

If it's a predetermined number,

validates:amount, numericality: {only_integer:true,less_than_or_equal_to:5000}

I think it would be good to do it like this, but
What should I do for to use the updated and constantly changing balance figures for validation like this time?

Table Status

create_table "bank_accounts", force: :cascade do | t |
  t. integer "user_id"
  t.string "account_number"
  t.decimal "balance"
  t.datetime "created_at", null:false
  t.datetime "updated_at", null:false
  t.index["user_id", name: "index_bank_accounts_on_user_id"
  end

User model.deDevice manages it.

class User<ApplicationRecord
  has_one —bank_account
  device:database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable
end

BankAccount model.

class BankAccount <ApplicationRecord
#BankAccount is owned by the user in a 1:1 relationship
belongs_to —user

# Account balance is valid only in numbers or decimal places
values:balance,presence:true,numberality:true
end

Input Form

<%=form_for @account_transaction do|f|%>
<div class="field">
  <%=f.label: Payment %>
  <%=f.number_field:amount, min:1, max:99999%>
  <%=f.submit "Confirm"%>
</div>
<%end%>

ruby-on-rails ruby validation

2022-09-30 18:25

2 Answers

If standard validators lack functionality, you can implement them yourself.

Simply put,

class Somehow <ApplicationRecord
  belongs_to —user
  validate:check_balance

  private
  def check_balance
    errors.add(:amount, "Insufficient balance"") if amount > user.bank_account.balance
  end
end

It's like this.

For more information, see Reference or Rails Guide.


2022-09-30 18:25

You may leave the intent of the question (you want to use changing values for validation), but
You can end up with the result that you want to achieve: "Only payments below the balance are valid."


"Only payments below the balance are valid" means
In other words,
I don't want to pay more than the balance and make the balance negative (the balance must be plus or zero),
"In other words, I was able to solve the problem with the simple and standard validation, ""If the balance is 0 or higher, the verification is successful."""

class BankAccount <ApplicationRecord
  #BankAccount is owned by the user in a 1:1 relationship
  belongs_to —user

  # Account balance can be numeric or decimal, and if the calculation results in a specified value of 0.00 or more (the balance does not become negative), verification is successful.
  values:balance,presence:true,numberality:{greater_than_or_equal_to:0.00}
end


2022-09-30 18:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.