Rails:验证唯一性时显示重复

时间:2014-03-19 14:16:01

标签: ruby-on-rails validation custom-errors

我有一个Rails模型,可以验证它的slug是唯一的,就像这样:

validates_uniqueness_of :slug

我在批量导入过程中遇到了很多验证失败,并出现如下错误:

ERROR: invalid () - Validation failed: Slug has already been taken
ERROR: invalid () - Validation failed: Slug has already been taken
ERROR: invalid () - Validation failed: Slug has already been taken
...

我想让错误消息显示哪些slugs验证失败。怎么办呢?

1 个答案:

答案 0 :(得分:1)

validates :slug, uniqueness: { message: "%{value} has already been taken" }

如果这是在日志中,我认为您将编写自定义验证并将其记录在那里。