Dean
Dean

Reputation: 8998

Do rails model validations take priority?

In a model I have the following validations:

validates_presence_of :company_name, :message => "Must have a company name"
validates_uniqueness_of :company_name, :message => "Does this company exist? As this company name isn't unique."

Does this mean that it will check for the presence of the company name before its uniqueness?

Upvotes: 2

Views: 271

Answers (3)

citraL
citraL

Reputation: 1156

Yes, the check will be done sequentially

You can check it with:

if obj.errors.on(:company_name)
    errMsg << obj.errors.on(:company_name)[0]
end

Upvotes: 2

Ziggurat
Ziggurat

Reputation: 257

From the rails guide: "validations will be run in the same order as they were registered."

Upvotes: 3

abhas
abhas

Reputation: 5213

Yes it will check presence first. It runs validations sequentially

Upvotes: 1

Related Questions