Use find_or_create_by to avoid duplicate records (#5092)

This commit is contained in:
Molly Struve 2019-12-12 12:01:15 -06:00 committed by GitHub
parent ac4e7eca90
commit dc5ec4e52f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@ class PollSkipsController < ApplicationController
before_action :authenticate_user!, only: %i[create]
def create
@poll_skip = PollSkip.create(poll_id: poll_skips_params[:poll_id], user_id: current_user.id)
@poll_skip = PollSkip.find_or_create_by(poll_id: poll_skips_params[:poll_id], user_id: current_user.id)
@poll = Poll.find(poll_skips_params[:poll_id])
render json: { voting_data: @poll.voting_data,
poll_id: poll_skips_params[:poll_id].to_i,