How do I check if a record already exists in my db when posting with Ajax?
How do I check if a record already exists in my db when posting with Ajax?
How do I check if a record already exists in my db when posting with Ajax?
Here is my Ajax code:
$.ajax({
type: "POST",
url: "team_selections#create",
data: {
team_selection: {
season_id: "1",
club_id: "1",
player_id: id,
fixture_week: "1",
position: pos
}
},
dataType: "html"
})
Here is my Rails controller code:
def create
if !TeamSelection.where(season_id: params[:season_id], club_id: params[:club_id], player_id: params[:player_id], fixture_week: params[:fixture_week], position: params[:position]).exists?
TeamSelection.create(selection_params)
end
end
private
def selection_params
params.require(:team_selection).permit(:season_id, :club_id, :player_id, :fixture_week, :position)
end
if
The controller should return some response saying record already exists. Currently, it's not returning anything.
– Imran
Jul 2 at 11:51
First, add a validation in model if you don't want similar records to be created more than once.
– Jagdeep Singh
Jul 2 at 11:56
3 Answers
3
you can use find_or_create_by rails method in your controller. this will finds the first record with the given attributes, or creates a record with the attributes if one is not found.This method always returns a record, but if creation was attempted and failed due to validation errors it won’t be persisted, you get what create returns in such situation.
def create
TeamSelection.find_or_create_by(selection_params)
end
You can add a check with the help of a before_action.
before_action
before_action :check_record_exists?
def create
TeamSelection.create(selection_params)
render_something
end
private
def check_record_exists?
if TeamSelection.where(selection_params.slice(:season_id, :club_id, :player_id, :fixture_week, :position)).exists?
render json: { error: 'Record already exists' }
end
end
def selection_params
params.require(:team_selection).permit(:season_id, :club_id, :player_id, :fixture_week, :position)
end
NOTE: You definitely need to have a validation on model to prevent creation of such records. Don't just rely on checks in controller or the JS.
As @Jagdeep commented correctly: add a validation in the model if you don't want similar records to be created more than once.
But here controller is not returning any response like 'Record already exists'
Replace your create method with
create
def create
is_record_present = TeamSelection.where(season_id: params[:season_id], club_id: params[:club_id], player_id: params[:player_id], fixture_week: params[:fixture_week], position: params[:position]).exists?
if !is_record_present
TeamSelection.create(selection_params)
else
#return respose for example
render json: {message: 'Record already present'}, status: :bad_request
end
end
Can I ask why adding it to a variable is any different to typing it outright as I did previously?
– lastone
Jul 2 at 11:58
I added that for readability, notice else part.
– Imran
Jul 2 at 11:59
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please specify which exact method/line is not working ? Does the query in the
ifcondition return the proper record if its present ?– Alok Swain
Jul 2 at 11:44