在使用Ajax发布时,如何检查数据库中是否已存在记录?

时间:2022-03-21 20:25:45

How do I check if a record already exists in my db when posting with Ajax?

在使用Ajax发布时,如何检查数据库中是否已存在记录?

Here is my Ajax code:

这是我的Ajax代码:

    $.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:

这是我的Rails控制器代码:

    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

3 个解决方案

#1


2  

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.

你可以在你的控制器中使用find_or_create_by rails方法。这将找到具有给定属性的第一条记录,或者如果找不到一条属性,则创建带有属性的记录。此方法始终返回记录,但如果尝试创建并且由于验证错误而失败则不会保留,在这种情况下获得创造回报的东西。

def create
  TeamSelection.find_or_create_by(selection_params)
end

#2


0  

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.

注意:您肯定需要对模型进行验证以防止创建此类记录。不要只依靠控制器或JS中的检查。

#3


0  

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

正如@Jagdeep正确评论:如果您不希望多次创建类似记录,请在模型中添加验证。但是这里控制器没有返回任何响应,如'Record already exists'替换你的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

#1


2  

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.

你可以在你的控制器中使用find_or_create_by rails方法。这将找到具有给定属性的第一条记录,或者如果找不到一条属性,则创建带有属性的记录。此方法始终返回记录,但如果尝试创建并且由于验证错误而失败则不会保留,在这种情况下获得创造回报的东西。

def create
  TeamSelection.find_or_create_by(selection_params)
end

#2


0  

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.

注意:您肯定需要对模型进行验证以防止创建此类记录。不要只依靠控制器或JS中的检查。

#3


0  

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

正如@Jagdeep正确评论:如果您不希望多次创建类似记录,请在模型中添加验证。但是这里控制器没有返回任何响应,如'Record already exists'替换你的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