Update:
Upon further examination, it appears that when the numbers are printed out, they are identical and only display two decimal places. Additionally, in my database, these values are stored as decimal type with precision 8 and scale 2.
Within my Rails application, I have a model called "pieces" which contains two fields: artist_cut and total_price. The edit form for each piece includes javascript that calculates the total_price based on the artist_cut input. To prevent users from editing the artist_cut field, I made it readonly.
However, readonly fields can be manipulated easily, so I implemented validation to ensure that the total_price matches the expected value. If not, an error is triggered.
The validation functions correctly for all artist_cuts except those falling between 3892 and 3898. In these cases, the comparison between the calculated javascript value and the Ruby value seems incorrect. How can I resolve this issue?
_form.html.erb
<div class="form-group">
<%= f.label :artist_cut %><br>
<%= f.text_field :artist_cut, class: "form-control", id: "artist_cut" %>
</div>
<div class="form-group">
<%= f.label :total_price %><br>
<%= f.text_field :total_price, class: "form-control", id: "total_price", readonly: true %>
</div>
<script>
$(document).on("change", "#artist_cut", function() {
var artist_cut = $("#artist_cut").val() || 0;
$("#total_price").val(Math.ceil((((artist_cut*2.19)+0.3)/(1-0.029))*100)/100);
});
</script>
piece.rb
validates :artist_cut, presence: true, format: { with: /\A\d+(?:\.\d{0,2})?\z/ }, numericality: { greater_than: 0.5, less_than: 443378.86 }
validates :total_price, presence: true, format: { with: /\A\d+(?:\.\d{0,2})?\z/ }, numericality: true
validate :total_price_validation
def total_price_validation
return if self.total_price == ((((self.artist_cut*2.19)+0.3)/(1-0.029))*100).ceil/100.0
errors.add(:total_price, "cannot be changed")
end
Thank you