Rails 4 - Validate Unique with Condition [duplicate] - ruby-on-rails

I'm trying to create a condition in which the attribute 'one' is zero and the attribute 'two' is one, then a model is not valid. But when I make:
Model.create(:one => 1, :two => 0).valid?
The unit test returns true! Why?
validates :one, :two, :presence => true, :if => :if condition_testing?
def condition_testing?
!(one == 0 && two == 1)
end

I think you have an error in your syntax:
validates :one, :two, :presence => true, :if => :condition_testing?
def condition_testing?
!(one == 0 && two == 1)
end
There was one :if too many in there...
And if I understand correctly you want to have it only validate in case one == 0 && two == 1?
Then you condition_testing? is inverted (leave out the !())
If unsure you could try to use pry and insert a breakpoint into your condition_testing? method to see what's going on.
(Please note added ":" before condition testing)

You can validate it in one line:
validates :one, :two, :presence => true, :if => Proc.new { |a| !(a.one == 0 && a.two == 1) }

You are better off using numericality and equal to.
validates :one, :numericality => { :equal_to => 0 }
validates :two, :numericality => { :equal_to => 1 }
http://guides.rubyonrails.org/active_record_validations_callbacks.html#numericality

The problem is that you're using a presence validator with a condition that checks the values of the attributes. This is incorrect. A presence validator checks to make sure those attributes are set. What's worse, you're passing the if option (#Tigraine was correct about your syntax being wrong, by the way), which means whenever that method returns true, the presence won't be checked at all. The way you have this set up, the validator will run only when one is equal to 1 and two is equal to 0. Otherwise, no validations are run at all! I think the best option here is to write a custom validation:
validates :one_and_two
def one_and_two
errors.add(:base, "one must be 1 and two must be 0") if !(one == 0 && two == 1)
end
This will add an error to the model with the specified message if the condition returns true. (Note: I'm still not clear on what condition is valid and which is invalid, so feel free to change that last part to suit your needs.)

Related

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 === "" }

Rails: Multiple if conditions in validation

Using: Rails 3.0.3
I have validations such as this one:
validates_numericality_of :person_weight_kg, :greater_than => 0, :message => "value_must_be_number_over_zero", :if => :bmi_calculation?, :if => :is_metric?
That I would like to validate for multiple if-conditions (such as in the example). But it seems, however, that Rails treats these statements as OR. One returns false and one returns true which makes the validation go through.
So, how do I check that this validation fulfills BOTH if-statements (bmi_calculation AND is_metric)?
Use a lambda as the if condition:
validates_numericality_of :person_weight_kg,
if: -> record { record.bmi_calculation? && record.is_metric? }

Rails: How to set different ":message" for each one of the possible errors?

I use this validation:
validates_numericality_of :price, :greater_than_or_equal_to => 0, :less_than => 1000000
How could I set a different :message for each one of the following cases ?
price < 0
price >= 1000000
Assuming you're using Rails 3, another option you have is to create a custom validator:
# You can put this in lib/better_numericality_validator.rb
class BetterNumericalityValidator < ActiveModel::EachValidator
def validate_each(record,attribute,value)
if value < 0
record.errors[attribute] << "must be greater than or equal to 0"
elsif value >= 1000000
record.errors[attribute] << "must be less than 1000000")
end
end
end
Then you can use your custom validator in your model:
# In your model.rb
validates :price, :better_numericality => true
This method is very similar to Anubhaw's answer. But pulling the logic out into the a custom validator makes it so that you can reuse the validation elsewhere easily, you can easily unit test the validator in isolation, and I personally think that validates :price, :better_numericality => true leaves your model looking cleaner than the alternative.
You can use following in model.rb:-
def validate
if self.price < 0
errors.add(:price, "custom message")
elsif self.price > 1000000
errors.add(:price, "custom message")
end
end
Thanks....
How about:
validates_numericality_of :price, :greater_than_or_equal_to => 0, :message => "Foo"
validates_numericality_of :price, :less_than => 1000000, :message => "Bar"
I've not tested it, but it should work?
Alternatively, Anubhaw's question is a good fallback.
At some point, you should probably ask yourself whether it isn't time to apply some convention over configuration.
In my opinion, an error message such as "Please enter a valid price greater than 0 and less than 1 million" (or similar) is a perfectly valid solution to the problem. It prevents you from adding unnecessary complexity to your application and allows you to move on to other (presumably more important) features.

How to return a true/false statement off of a select conditional in rails

I am trying to select an array, and check and see if any of the objects within the array are false. If any of them are false, I want an ultimate value of false returned.
If all of them are true, I want true returned..
Here's what I have..
validates_presence_of :email, :if => Proc.new { |user| user.organizations.find(:all).select {|org| org.contact_24} }
This unfortunately just returns the array.
How would you do it?
Perform the check in the DB, using the exists? method. This ensures all the calculations are done at the DB rather than the client code.
validates_presence_of :email,
:unless => Proc.new { organizations.exists?(:contact_24 => false)}
If you still insist on performing this at the client side then:
validates_presence_of :email,
:unless => Proc.new { organizations.any?{|o| o.contact_24 != true}}
OK, so your proc:
org.contact_24
Select will just return an array... So you need to return true if all org.contact_24 are true.
validates_presence_of :email, :if => Proc.new { |user| user.organizations.find(:all).collect {|org| org unless org.contact_24}.compact.blank? }
That'll build an array of org's that have contact_24 that are false. It then compacts the array, and returns true if it's blank.
So, it'll be false if any records aren't true.
I'd recommend moving the organizations.find(:all).collect {|org| org unless org.contact_24}.compact.blank? into a scope, so you'd end up with:
user.organizations.not_contacted_in_24_hours
Wow that is over complicating a simple task. How about
def some_func(arr)
!arr.include?(false)
end
Maybe I am missing something... but the question just asks how to return 'false' is an array includes 'false', and that's what this does.

Rails greater_than model validation against model attribute

I've got a Trip model, which among other attributes has a start_odometer and end_odometer value.
In my model, i'd like to validate that the end odometer is larger than the starting odometer.
The end odometer can also be blank because the trip may not have finished yet.
However, I can't figure out how to compare one attribute to another.
In trip.rb:
comparing against the symbol:
validates_numericality_of :end_odometer, :greater_than => :start_odometer, :allow_blank => true
gives me the error:
ArgumentError in TripsController#index
:greater_than must be a number
comparing against the variable:
validates_numericality_of :end_odometer, :greater_than => start_odometer, :allow_blank => true
NameError in TripsController#index
undefined local variable or method `start_odometer' for #
You don't necessarily need a custom validation method for this case. In fact, it's a bit overkill when you can do it with one line. jn80842's suggestion is close, but you must wrap it in a Proc or Lambda for it to work.
validates_numericality_of :end_odometer, :greater_than => Proc.new { |r| r.start_odometer }, :allow_blank => true
You'll probably need to write a custom validation method in your model for this...
validate :odometer_value_order
def odometer_value_order
if self.end_odometer && (self.start_odometer > self.end_odometer)
self.errors.add_to_base("End odometer value must be greater than start odometer value.")
end
end
You can validate against a model attribute if you use a Proc.
In rails4 you would do something like this:
class Trip < ActiveRecord::Base
validates_numericality_of :start_odometer, less_than: ->(trip) { trip.end_odometer }
validates_numericality_of :end_odometer, greater_than: ->(trip) { trip.start_odometer }
end
It's a bit kludgy, but this seems to work:
validates_numericality_of :end_odometer, :greater_than => :start_odometer.to_i, :allow_blank => true

Resources