problem with passing booleans to update_attributes - ruby-on-rails

I've got the following Model:
class GuestCatering < ActiveRecord::Base
# Validation
validates :name, :presence => true
validates :order_number, :presence => true
validates :orderable, :presence => true
end
But when I'll try to update an existing GuestCatering with the following code:
guest_catering.update_attributes(:orderable => false)
The guest catering variable is a valid GuestCatering object.
The guest_catering object has errors after the update, like that:
<{[:orderable, ["can't be blank"]]=>nil}>
But when i pass a orderable => true, everything is fine and no errors.
What's wrong here, why can't i set orderable to false?

Your model is actually behaving exactly as you told it to, through your use of validates :orderable, :presence => true
There's little point validating the presence of a boolean flag - it's going to be true, nil or false - and in Ruby world, nil and false have the same semantic value when it comes to boolean logic.
Internally, validates :presence relies on the value of the attribute being checked to return false when blank? is called. And, in Rails (with ActiveSupport), false.blank? evaluates as true - which means that your field is failing the validation.
Simply remove that validation and everything will work as expected.

Like Dan Cheail already said in his answer, a nil and false boolean is semantically the same thing.
But, if you really need to validate it (not allowing nil), you can always do :
validates_inclusion_of :orderable, :in => [true, false]

Instead of validates :presence => :true, you should write your migrations with the default value like this:
t.boolean :orderable, :default => 0
I assume your default value should be false. If true, use 1 as default. Then it will set the default value in database. So, you can omit the validation check.
The reason you cannot use validates :presence is answered by #dan. Presence means not blank and Rails use .blank? function for this and false.blank? is true

Related

Minitest for testing presence and allow_nil for the same attribute

I want to test a model attribute where it has presence as true, but also allow nil value. How can I test this? I made an example bellow:
# Person model
belongs_to :city
validates :city_id, presence: true, :allow_nil => true
I was trying to test with:
test "should permit nil for city_id" do
#person.city_id = nil
assert #person.valid?
end
What I got in console:
Expected false to be truthy.
I found the answer. The test was failing because it was missing a argument in belongs_to:
# Person model
belongs_to :city, optional: true
validates :city_id, presence: true, :allow_nil => true
The argument Optional allows nil value to foreign key.

Mongomapper uniqueness

I have user model
class User
include MongoMapper::Document
key :phone, Integer, :required => true, :unique => true
key :is_confirmed, Boolean, :default => false
timestamps!
end
and validate uniqueness of phone but i can create user with the same phone without error.WHY?
why validate uniqueness doesn't work
MongoMapper uses ActiveModel:Validations, so it works almost exactly like ActiveRecord
Try this to validate: validates_uniqueness_of
validates_uniqueness_of :phone
Validations are run when attempting to save a record. If validations fail, save will return false.
Most Simple Validations can be declared along with the keys.
Example:
class Person
include MongoMapper::Document
key :first_name, String, :required => true
key :last_name, String, :required => true
key :age, Integer, :numeric => true
key :born_at, Time
key :active, Boolean
key :fav_colors, Array
end
The available options when defining keys are:
:required – Boolean that declares validate_presence_of
:unique – Boolean that declares validates_uniqueness_of
:numeric – Boolean that declares validates_numericality_of
:format – Regexp that is passed to validates_format_of
:in – Array that is passed to validates_inclusion_of
:not_in – Array that is passed to validates_exclusion_of
:length – Integer, Range, or Hash that is passed to validates_length_of

validation only if a drop down value is not nil

I am trying to perform a validation on a field only if a different field (a drop down) in the form is not nil.
my validation check if a phone number has 10 digits, is unique and presence only if the provider is not nil
validates :phoneNumber,length: {is: 10}, uniqueness: true, presence: true, if: "!provider.nil?"
my form
<%= f.select :provider, options_for_select(%w[AT&T T-Mobile Verizon]), :prompt => "Select Cellular Provider",:selected => nil%>
no matter what I do this thing always performs the check of validation even if I did not select a value from my drop down.
According to the rails guide I can set selected to nil, and can perform an if validation
Any idea where am I getting this wrong?
thank you for your help
After chatting with you I think you need, to continue validating length, presence and uniqueness, and in addition the case you mentioned:
validates :phoneNumber,length: {is: 10}, uniqueness: true, presence: true, if: lambda{ |record| record.provider.present? }
validates :provider, presence: true, if: lambda{ |record| record.phoneNumber.present? }

Rails Field Validation Based on Value of Another Field

I need to validate that the value of a field (:discount) is one of an array of strings. The :discount field can also be blank UNLESS the :type field is 'FixedDeal' (STI)
validates :discount, inclusion: {in: VALID_DISCOUNTS}, allow_blank: true unless :type == 'FixedDeal'
The above code works to validation the value of :discount, but allows the field to be blank even if the type is 'FixedDeal'.
On your FixedDeal class add the following validation:
validates_presence_of :discount
That should work for you.

Where do I put the database constraints in rails?

Simple problem. I'm learning RoR. I swear that I searched this theme here and in google.
I need a lot of tables in my app.
I'm reading about the benefits of database constraints. I'm using validations inside every model, example:
class Example < ActiveRecord::Base
belongs_to :other
has_one :another...
attr_accessible :username, :email, :password
validates :username, e:mail, :password, presence: true
validades .....
end
I would like to know about database constraints, how can i get the same validation inside the database? Should i put this constraints (like :null => false) inside the schema.rb file?
Yes, absolutely put that in your migration:
:null => false
To require a non-empty field. Although an empty string can still be supplied and it passes the non-NULL test. You can cover this by adding a length validation:
validates_length_of :username, :minimum => 1, :maximum => 255

Resources