最佳答案
如果你查看 lib/mongoid/finders.rb 中的源代码:
# Find the first +Document+ given the conditions, or creates a
# with the conditions that were supplied.
...
# @param [ Hash ] attrs The attributes to check.
#
# @return [ Document ] A matching or newly created document.
def find_or_create_by(attrs = {}, &block)
find_or(:create, attrs, &block)
end
您可以看到 find_or_create_by 接受 {}
作为第一个参数。你可以一次传入多个条件
something.find_or_create_by(name: 'john', age: 20)
它应该可以工作。
关于ruby-on-rails - Rails 3 find_or_create 由多个属性 mongoid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11985325/