1

I have the following model in Rails which manages a Diabetes Settings table:

class DiabetesSetting < ApplicationRecord
    belongs_to :user

    validates :bs_min, :bs_max, :bs_target, :correction_number, numericality: { only_integer: true }
    validates :correction_insulin, numericality: true
    validates :bs_min {less_than: :bs_max }
    validates :bs_max {greater_than: :bs_min}
    validates :bs_target {less_than: :bs_max}

end

My Question: Is something like this, to compare the fields of a database with each other to validate, that when a new setting is created, one value is higher or lower than the other, possible in Rails or do I have to write custom validation methods for this?

To clarify: bs_min is the mimmum value of a numerical range and bs_max the max value of a numerical range. They are both attributes inside the diabetes settings model. The Question is if I can compare these values like I did or if I have to write custom methods to validate

Thanks in advance for Your help

4
  • 1
    Can you describe more details about what actually you want to validate? Commented Jan 24, 2018 at 14:54
  • @Tai sure. I updated the Question. The main question is , though is it possible to validate the way I did or do I have to write custom validation methods? Thank You :-) Commented Jan 24, 2018 at 15:01
  • 1
    you'll have to do custom validators Commented Jan 24, 2018 at 15:05
  • @Anthony Shit, I thought so ;-) Would have been to good to be true if it would work the way I wrote it above Commented Jan 24, 2018 at 15:07

2 Answers 2

1

You can do it using Rail's built-in validations. Try below code:

class DiabetesSetting < ApplicationRecord
  ...
  validates_numericality_of :bs_min,    { less_than: :bs_max }
  validates_numericality_of :bs_max,    { greater_than: :bs_min }
  validates_numericality_of :bs_target, { less_than: :bs_max }
end

Refer to following link for numerical validation: http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_numericality_of

Sign up to request clarification or add additional context in comments.

4 Comments

Have you tried it? Which Rails version are you using?
Yes, I tried. Rails 5.0.0. Validate it in Rails console.
Hey thanks for the effort but this is not working. When testing the model with unit tests and and setting up a valid instance of the model, the test fails.
@Anthony was right. When comparing data fielsd with each other inside validations you need to write custom validators.
1

Ruby on Rails 7.0 added support for validates_comparison_of validations like this

validates :bs_min, comparison: { less_than: :bs_max }
validates :bs_max, comparison: { greater_than: :bs_min }
validates :bs_target, comparison: { less_than: :bs_max }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.