現在の値が既にデータベースにある場合は、現在の値を置き換えるオプションを使用して、データベースにある種の一意性を実装しようとしています。
私のテーブルは次のようになります
t.integer "task_id"
t.integer "accessor_id"
t.integer "access_owner"
t.boolean "access_rights"
そして、access_rightを更新するよりも、task_id、accessor_id、およびaccess_ownerを持つエントリがあるかどうかを確認し、ない場合はそのようなエントリを作成します
私の現在の作成方法と新しい方法は
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)
しかし、それはうまくいきませんでした