Reputation: 6383
I have a column that can be either positive or negative, but never zero. I'm doing the following which doesn't seem to work:
class Transaction < ActiveRecord::Base
validates :amount, presence: true
validates :amount, :numericality => { :not_equal_to => 0 }
.
.
.
This is my test
let(:transaction) { FactoryGirl.build(:transaction) }
it "is invalid if amount is zero" do
transaction.amount = 0
expect(transaction).to have(1).error_on(:amount)
end
This is my factory:
FactoryGirl.define do
factory :transaction do
sequence(:description) { |n| "Transaction #{n}" }
category_id nil
amount -100
notes nil
fund_id 1
end
end
What's the way to check number is either positive or negative? Is there something else I'm doing wrong?
Thanks
Upvotes: 0
Views: 921
Reputation: 124419
You want :other_than
, not :not_equal_to
validates :amount, :numericality => { :other_than => 0 }
See the documentation on validates_numericality_of
for all the options.
Upvotes: 4