jeudi 19 juillet 2018

first_or_create only insert

I'm using the following code in a Rails 5.2 project:

c = Country.where(name: "test").first_or_create

c.status = "Old"
c.save

This works, however, I only want to change the status when there is not a country already (so I only want to insert countries, not update). However, I also need to use c later in my code.

I realize I could just do an if statement, but I have to do a similar thing multiple times in my code, and it seems like a shortcut must exist.

Aucun commentaire:

Enregistrer un commentaire