randombits
randombits

Reputation: 48440

Rails 2.3.x: possible to combine these validations into one?

Is it possible to keep things DRY and put this into one validation line?

  validates_presence_of     :login
  validates_uniqueness_of   :login

Upvotes: 2

Views: 104

Answers (2)

Ryan Bigg
Ryan Bigg

Reputation: 107718

In Rails 3 (which is not released yet, but please do try out the beta) you can!

validates :login, :presence => true, :uniqueness => true

Upvotes: 0

x1a4
x1a4

Reputation: 19475

You actually can just remove the validates_presence_of line, because validates_uniqueness_of defaults to :allow_blank => false (and :allow_nil => false)

Take a look at the docs here .

Upvotes: 5

Related Questions