如何在 save() 期间捕获 ActiveRecord::RecordNotFound 异常?
我的数据库中有一个包含电子邮件列的用户表。我还在“电子邮件”列上创建了一个 UNIQUE 索引,以防止两个用户注册相同的电子邮件地址(注意:请不要建议我使用 validates_uniqueness_of
因为这就是我想要的避免)。
当我运行 RSpec 测试以确保无法插入重复记录时,我看到以下错误:
Failures:
1) User should not allow duplicate email addresses
Failure/Error: user2.save.should_not be_true
ActiveRecord::RecordNotUnique:
SQLite3::ConstraintException: column email is not unique: INSERT INTO "users" ("email", ... ) VALUES ( ... )
# ./spec/models/user_spec.rb:26
这很好,因为这意味着我的 UNIQUE 索引确实正在工作。问题是,我该如何处理这个异常?我希望能够捕获它,然后将合理的消息添加到模型的错误集合中。
我尝试过 - 不成功 - 在控制器中使用rescue_from,如下所示:
rescue_from 'ActiveRecord::RecordNotUnique' do |ex|
raise 'Email must be unique'
end
Rails API文档似乎没有建议如何重写 save() 方法以添加开始/救援块,所以我的问题是:如何我可以处理在 save() 期间引发的 ActiveRecord::RecordNotUnique 异常,然后将模型标记为无效并向模型的错误集合添加合理的错误消息吗?
I've got a Users table with an Email column in my database. I've also created a UNIQUE index on the Email column to prevent two users from registering the same email address (note: please don't suggest that I use validates_uniqueness_of
since this is what I'm trying to avoid).
When I run run my RSpec test to make sure that a duplicate record cannot be inserted, I see the following error:
Failures:
1) User should not allow duplicate email addresses
Failure/Error: user2.save.should_not be_true
ActiveRecord::RecordNotUnique:
SQLite3::ConstraintException: column email is not unique: INSERT INTO "users" ("email", ... ) VALUES ( ... )
# ./spec/models/user_spec.rb:26
This is good because it means that my UNIQUE index is indeed working. The question is, how can I handle this exception? I'd like to be able to catch it, then add a sensible message to the model's errors collection.
I've tried - unsuccessfully - using rescue_from in the controller as follows:
rescue_from 'ActiveRecord::RecordNotUnique' do |ex|
raise 'Email must be unique'
end
The Rails API docs don't appear to suggest how to override the save() method in order to add a begin/rescue block, so my question is this: How can I handle the ActiveRecord::RecordNotUnique exception that is being thrown during save() then mark the model as invalid and add a sensible error message to the model's errors collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以重载模型中的任何操作,只需调用
super
来执行继承的方法定义Rails API 很可能没有提及它,因为它是 Ruby 的功能,而不仅仅是 Rails 的功能。
You can overload any action in your models and just call
super
to execute the inherited method definitionThe Rails API prolly doesn't mention it because its an feature of Ruby, not just Rails.
我有类似的问题。我有一个带有索引的表,该索引使用多个字段,该表
在 db/migrate 处排序
现在,验证 中字段的唯一组合>models/did.rb 我写道:
但是,它没有验证重复的字段组合(lada+pre_did+did),所以在models/did.rb还写道:
现在就我而言,如果我在救援后不返回false,这是行不通的。
I had a similar problem. I have a table with an index using several fields up by wich the table is sorted
at db/migrate
Now, to validate a unique mix of fields in models/did.rb I wrote:
But, it did not validate for duplicated mix of fields (lada+pre_did+did), so in models/did.rb also wrote:
Now in my case, if I do not return false after rescue, this does not work.