我试图在我的数据库中实现某种唯一性,如果它已经在数据库中,则可以选择替换当前值。
我的桌子看起来像
t.integer "task_id"
t.integer "accessor_id"
t.integer "access_owner"
t.boolean "access_rights"
我想检查是否有一个带有 task_id、accessor_id 和 access_owner 的条目而不是更新 access_right,如果没有创建这样的条目
我目前的创建方法和新方法是
class AccessorsController < ApplicationController
def new
@accessor = Accessor.new
@task_id = params[:task_id]
end
def create
@accessor = Accessor.new(accessor_params)
@user = User.where(email: params[:accessor][:accessor_id]).first
@accessor.accessor_id = @user.id
@accessor.access_owner = current_user.id
respond_to do |format|
if @accessor.save
format.html { redirect_to Task, notice: 'User was successfully created.' } #
format.json { render action: 'show', status: :created, location: Task }#
else
format.html { render action: 'new' } #
format.json { render json: @accessor.errors, status: :unprocessable_entity }#
end
end
end
我尝试执行以下操作
@accessor = Accessor.where("access_owner = ? AND accessor_id = ? AND task_id = ?", current_user.id, User.where(email: params[:accessor][:accessor_id]).first,params[:task_id]).first || Accessor.new(accessor_params)
但它没有用