AnApprentice
AnApprentice

Reputation: 110950

Rails 3 - How to Create a Record if One Does not Exist OR Update the existing records

I'm creating a permissions table for People & Books

In the permissions table I have: Permission.ID, user.id, book.id

I want an admin to be able to set permissions for Users<>Books.

When the user selects the permissions and clicks submit, in the Rails controller, should I be submitting to /create or /update?

is there a way I can submit to just one whether it's new or an update, and let Rails know to either Create or Update a record based on if a record exists per a UserID And BookID?

Thanks

Upvotes: 2

Views: 2285

Answers (1)

Jarrod
Jarrod

Reputation: 2378

If they're editing an existing record just submit to update. If they are creating a new record and you're making sure there aren't any duplicates, try using find_or_create_by inside your create method.

Upvotes: 2

Related Questions