我的产品模型在下面有这个验证规则,在测试规则时,我发现 uniqueness: true for :title 实际上什么也没做。
validates( :title, presence: {message: ' must be given'}, uniqueness: true )
例如,如果我像这样创建两个具有相同标题的实例,
a = Product.new title: 'title', description: 'hello!!', user: User.find(39)
a.save
id | title | description | price | created_at | updated_at | user_id |
+-----+-------+-------------+-------+--------------------+-------------+
162 | title | hello!! | 0.0 | 2018-... | 2018-02... | 39 |
b = Product.new title: 'title', description: 'hahah', user: User.find(39)
b.save
id | title | description | price | created_at | updated_at | user_id |
+-----+-------+-------------+-------+--------------------+-------------+
163 | title | hahah | 0.0 | 2018-... | 2018-02-2... | 39 |
我不明白为什么唯一性根本不起作用?