drumwolf
drumwolf

Reputation: 409

How do I set up numericality for a Rails model attribute so it will accept any non-zero positive or negative value?

I'm building a Ruby on Rails app, and one of my models is called "Asset" with an attribute called "shares." At first, I thought I only wanted "shares" to have a value higher than zero.

class Asset < ActiveRecord::Base
  attr_accessible :shares, .....
  ......
  validates :shares,  presence: true, numericality: { :greater_than => 0 }
end

Now it turns out that I want "shares" to be able to have either positive or negative value. The only value it CAN'T have is zero.

Is there a way for me to set "numericality" so it can be either greater than or less than zero? Something along the lines of:

numericality: { :greater_than => 0, :less_than => 0  }

or

numericality: { :greater_than => 0 || :less_than => 0  }

Thanks!

Upvotes: 1

Views: 489

Answers (1)

J&#248;rgen Fogh
J&#248;rgen Fogh

Reputation: 7646

You can use :other_than => 0. All other numbers are either positive or negative.

Upvotes: 2

Related Questions