加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3 find_or_create由多个属性mongoid组成

发布时间:2020-12-17 03:28:50 所属栏目:百科 来源:网络整理
导读:在此链接中,Rails find_or_create by more than one attribute?可以使用多个具有活动记录的属性. 如何在mongoid中使用多个属性? 谢谢 解决方法 如果你看一下lib / mongoid / finders.rb中的源代码: # Find the first +Document+ given the conditions,or c
在此链接中,Rails find_or_create by more than one attribute?可以使用多个具有活动记录的属性.

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

谢谢

解决方法

如果你看一下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)

它应该工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读