为什么没有find_or_create_by!有类蒙古的方法?

时间:2013-11-05 12:33:39

标签: mongoid

Mongoid提供方法createcreate! 比如

Artist.create(name: "Pablo Picasso")

Artist.create!(name: "Pablo Picasso")

Mongoid还提供了一种称为find_or_create_by的方便方法 比如

a = Artist.find_or_create_by(name: "Pablo Picasso")

看来Mongoid应该提供一种方法find_or_create_by! 如果验证失败并且无法创建文档,则引发异常。

我知道使用Mongoid 3.1.0,你可以做到

Artist.where(name: "Pablo Picasso").first_or_create

Artist.where(name: "Pablo Picasso").first_or_create!

但它们不等同于find_or_create_byfind_or_create_by!(如果存在?)?

find_or_create_by语法要短得多,因此更好......

1 个答案:

答案 0 :(得分:1)

你是对的,现在没有find_or_create_by!方法。请打开一个新问题https://github.com/mongoid/mongoid/issues/new,我会在有空的时候尽快添加。

感谢您提出要求。

相关问题