Rails 基于两个字段查找或创建 [英] Rails find or create based on two fields

查看:62
本文介绍了Rails 基于两个字段查找或创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场地模型,我想做这个

I have a venue model and i want to do this

Venue.find_or_create_by_

但我只想创建一个新场地,如果名称和日期相同的场地不存在

but i only want a new venue to be created if one with the same name and date do not already exist

例如

=> Venue(id: integer, location: string, showdate: datetime, created_at: datetime, updated_at: datetime)

场地是唯一的,如果地点和演出日期不存在于数据库中,则需要创建场地

A venue is unique and needs to be created if the location and the showdate are not present in the db

推荐答案

您可以使用 _and_ 将列链接在一​​起.这应该可以解决问题:

You can chain columns together by using _and_. This should do the trick:

Venue.find_or_create_by_location_and_showdate(location, showdate)

这篇关于Rails 基于两个字段查找或创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆