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 admi开发者_JAVA技巧n 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
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.
精彩评论