validates with :if condition failed in rspec - ruby-on-rails

Having a rfq model in the app. There are two fields. One is need_report which is a boolean. Another one is report_language which is a string. The logic is if need_report is true, then there should be an entry in report_language. Otherwise, if need_report if false, report_language could be empty. Here is the code in rfq.rb:
validates :need_report, :presence => true
validates_inclusion_of :need_report, :in => [true, false]
validates :report_language, :presence => {:if => :need_report?}
def need_report?
need_report
end
However the following rspec case failed:
it "should be OK for nil report_language if need_report is false" do
rfq = Factory.build(:rfq, :need_report => false, :report_language => nil)
rfq.should be_valid
end
The error is that the rfq is not valid:
1) Rfq should be OK for nil report_language if need_report is false
Failure/Error: rfq.should be_valid
expected valid? to return true, got false
# ./spec/models/rfq_spec.rb:57:in `block (2 levels) in <top (required)>'
This case could pass if "validates :need_report, :presence => true" is removed from the model. It seems that if need_report is true, then report_language can not be empty.
Any thoughts about the problem? Thanks.

You can't use validates_presence_of or validates :column, :presence => true to check if boolean columns are empty.
http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_presence_of
Answer: Instead you need to use validates_inclusion_of and specify an array of accepted inputs, which you already have. This should be sufficient validation for what you want to do.
Explanation: Your first validation is seeing false in the column (which, in Ruby is equivalent to nil). It then runs .blank? on nil and gets back true (false == nil & nil.blank? == true), meaning it thinks the column is blank and it throws an error.

Related

Conditional case_sensitive validation in model

I have one model validation like below
validates :value, presence: true, allow_blank: false, uniqueness: { scope: [:account_id, :provider] }
I want to add one more condition of case_sensitive inside uniqueness like below
validates :value, presence: true, allow_blank: false, uniqueness: { scope: [:account_id, :provider], case_sensitive: :is_email? }
def is_email?
provider != email
end
In short, it should not validate case_sensitive when email provider is not email, But currently, it's not working it is expecting true or false only not any method or any conditions.
How can I achieve this in rails? I already wrote custom validation because it was not working.
UPDATE
if I add another validation like below
validates_uniqueness_of :value, case_sensitive: false, if: -> { provider == 'email' }
It's giving me same error for twice :value=>["has already been taken", "has already been taken"]
In the specific case of case_sensitive, the value passed to the option will always be compared against its truthy value.
As you can see in the class UniquenessValidator, when the relation is built, it uses the options passed to check if the value of case_sensitive is truthy (not false nor nil), if so, it takes the elsif branch of the condition:
def build_relation(klass, attribute, value)
...
if !options.key?(:case_sensitive) || bind.nil?
klass.connection.default_uniqueness_comparison(attr, bind, klass)
elsif options[:case_sensitive] # <--------------------------------- sadly, this returns true for :is_email?
klass.connection.case_sensitive_comparison(attr, bind)
else
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
klass.connection.case_insensitive_comparison(attr, bind)
end
...
end
As you're passing the method name is_email? to case_sensitive, which is in fact a symbol, the condition takes that branch.
tl;dr;. You must always use true or false with case_sensitive.

How to add model validation with condition in Ruby

I am trying to do model validation for gstno only if the selected country equals India. I tried like this not working:
validates :gstno, uniqueness: true, :format => {:with => /[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}/, :message => 'INCORRECT FORMAT!'} if self.country =='IN'
The error is like this:
undefined method `country' for #<Class:0x007fb021d6a0c8> Did you mean? count
Can you please try the following code:
validates :gstno, uniqueness: true, :format => {:with => /[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}/, :message => 'INCORRECT FORMAT!'}, if: -> { country == 'IN' }
You probably want to move this to a custom validation method
https://guides.rubyonrails.org/active_record_validations.html#custom-methods
self will be available to you there

Proc command is not working on checking the inclusion

I have a field called visit_time with two distinct values. They are "AM" and "PM"
I check the presence of the visit_time by the following validation syntax.
validates_presence_of :visit_time,
message: "visit time is required"
Then I need to check the inclusion validation only if the visit_time is presence, for this I am using the Proc. But it is not working.
validates :visit_time,
:inclusion => { :in => [ 'AM', 'PM'],
:message => "%{value} is not a valid time" },
:if => Proc.new { |o| o.errors.empty? }
Let me know what's wrong on it. Is Proc is not working for inclusion ??? Thanks in advance.
If you want the inclusion validation to run only if it's present, you should change the Proc to this instead:
if: Proc.new { |o| o.visit_time.present? }

Rails 4 rspec test failing on numericality only_integer on boolean value?

I have in my model
validates :is_foo, presence: true, numericality: {only_integer:true, less_than_or_equal_to: 1, greater_than_or_equal_to: 0}
in my spec
describe "when is_foo is not an integer" do
before {#user.is_foo = true}
it {should_not be_valid}
end
The above test fails, but since I set is_foo to a boolean and not an integer, should the test pass?
Are booleans considered integers in ruby? Because when I did
true.to_i # error
true == 1 # false
false == 0 # false
The spec is correct. 1 is truthy (the only non-truthy values in Ruby are false and nil), but it is not a boolean. Booleans are not numeric; they are their own type. A numericality validation will validate that the type is actually numeric, and boolean is not numeric.
> true.class
=> TrueClass
> 1.class
=> Fixnum
What you probably want instead, though, is a validation that tests for a boolean value:
validates :is_foo, inclusion => {:in => [true, false]}
I guess ruby/rails substitutes 1 for true to a model class attribute when the attribute expects an integer. So the test failed because it was comparing 1 to an integer.

In ActiveRecord, validate that an attribute is present but may be empty

I'm trying to set up my model in Rails 3.2.8 such that particular values must be present, but are allowed to be the empty string. Does anyone know how to do this?
The behavior I'm looking for looks like:
#foo.value = "this is a real value"
#foo.valid? # => true
#foo.value = nil
#foo.valid? # => false
#foo.value = ""
#foo.valid? # => true
If I use
validates :foo, :presence => true
then I get what I want for 1 and 2, but not 3. And, helpfully, :allow_blank => true is ignored when validating presence.
I also tried
validates :foo, :length => { :minimum => 0 }, :allow_nil => false
Same behavior: get what I want for 1 and 2, but not 3.
I suppose I could just mark the column as NOT NULL in the database, but then I have to deal with catching the exception; I'd really rather catch this at the validation stage.
Any ideas?
I would try something like this:
validates :foo, presence: true, unless: lambda { |f| f.foo === "" }

Resources