ruby on rails - Validation error involving numericality -


i working validations first time , i'm trying validate amount_spent , no_of_purchases field takes in integers , not strings. 200 valid 'two-hundred' wouldn't be. however, when try test, string part fails i'm unsure why. here snippet of rspec test file:

it 'requires # of purchases'   customer = customer.new(valid_customer.merge(no_of_purchases: ''))   customer_2 = customer.new(valid_customer.merge(no_of_purchases: 0))   customer_3 = customer.new(valid_customer.merge(no_of_purchases: 'twenty'))   expect(customer).to_not be_valid   expect(customer.errors[:no_of_purchases]).to include "can't blank"   expect(customer_2).to be_valid   expect(customer_3).to_not be_valid   expect(customer_3.errors[:no_of_purchases]).to include "is not number" end  'requires amount spent'   customer = customer.new(valid_customer.merge(amount_spent: ''))   customer_2 = customer.new(valid_customer.merge(no_of_purchases: 0))   customer_3 = customer.new(valid_customer.merge(no_of_purchases: 'twenty'))   expect(customer).to_not be_valid   expect(customer.errors[:amount_spent]).to include "can't blank"   expect(customer_2).to be_valid   expect(customer_3).to_not be_valid   expect(customer_3.errors[:no_of_purchases]).to include "is not number" end 

here model file:

 validates_presence_of :first_name  validates_presence_of :last_name  validates_presence_of :email  validates_presence_of :no_of_purchases, numericality: true  validates_presence_of :amount_spent, numericality: true 

i'm not seeing error. i've specified numericality true shouldn't validate string. thing problem i've placed default value 0 in schema file. i'm pretty sure problem because when use binding.pry customer 3 has no_of_purchase, amount_spent 0 instead of 'twenty'.

question 1: why that? question 2: how fix it?

thanks help.

at first glance can see you're using validates_presence_of incorrectly. unsure version of rails using, following answer relevant rails 3 , 4.

lets focus on these 2 lines:

validates_presence_of :no_of_purchases, numericality: true validates_presence_of :amount_spent, numericality: true 

the explicit validates_something_of validations perform one, , one, validation check @ time. validates_presence_of, asking rails check presence of attribute , nothing else. appending numericality: true passes in hash of options ignored. why numericality check not working in example code.

to work, can use built in validates method apply numericality validator both of attributes above:

validates :no_or_purchases, :amount_spent, numericality: true 

note didn't add explicit presence validation. built numericality check default (since nil not number!).

hope helps. also, rails validation's documentation explains how this, , many other built in validations, too.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -