Rails 3 find_or_create由多个属性mongoid组成

hyp*_*jas 5 ruby ruby-on-rails mongodb mongoid ruby-on-rails-3

在这个链接中,Rails find_or_create由多个属性组成?可以使用多个具有活动记录的属性.

如何在mongoid中使用多个属性?

谢谢

Sha*_*uli 6

如果你看一下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
Run Code Online (Sandbox Code Playgroud)

你可以看到find_or_create_by接受a {}作为第一个参数.你可以一次传递几个条件

something.find_or_create_by(name: 'john', age: 20)
Run Code Online (Sandbox Code Playgroud)

它应该工作.