ActiveRecord 'destroy' method returns a boolean value in Ruby on Rails?
I am using Ruby on Rails 3 and I would like to know what type of return will have the following code:
@user.destroy
I need that to handle cases on success and fault in someway like this:
if @user.destroy
puts "True"
else
puts "false"
end
Is it possible? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
You should try what you're asking before asking. What you've got there will work just fine.
If the destroy works, it will return the object (which will pass as true in an if statement) and if not, it will return false or raise an exception, depending on why it failed.
It's possible. The destroy method returns the object that was destroyed;
you could use
destroyed?
on this ActiveRecord object to check if the object is effectively destroyed: