Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails find or create based on two fields

Tags:

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

For example

=> 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

like image 549
Matt Elhotiby Avatar asked Oct 16 '10 17:10

Matt Elhotiby


1 Answers

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

Venue.find_or_create_by_location_and_showdate(location, showdate) 
like image 64
vonconrad Avatar answered Sep 20 '22 14:09

vonconrad