使用'if','&&'和'||'在一项条件声明中

时间:2019-06-16 18:21:57

标签: ruby-on-rails ruby

所以我要完成的是@ provider.licenses.where(issuing_state:'CA')'是否也必须与@ form.state相匹配,在这种情况下,为了获得访问该页面。现在,如果provider.licenses.issuing_state与@ form.state不匹配,则将重定向用户。这就是我所拥有的

def edit
        @user = current_user
        @patient = Patient.find(params[:patient_id])
        @form_response = FormResponse.find_by_id(params[:id])
        @form = @form_response.form
        @provider = current_user.try(:provider)
        @provider_user = @provider.try(:role).try(:user)

        if current_user.is_provider?
            if @provider.licenses.where(issuing_state:'CO') && @form.state == 'CA' || @provider.licenses.where(issuing_state:'CA') && @form.state == 'CO' 
                redirect_to root_path, alert: 'You do not have access to update form'

            end 
        end
    end

1 个答案:

答案 0 :(得分:1)

@provider.licenses.where(..)之类的东西总是真实的,因为它返回了ActiveRecord::Relation

您可能想改用@provider.licenses.exist?(issuing_state:'CO')

if @provider.licenses.exist?(issuing_state:'CO') && @form.state == 'CA' ||
   @provider.licenses.exist?(issuing_state:'CA') && @form.state == 'CO'
  redirect_to root_path, alert: 'You do not have access to update form'
end