私はこの質問が何度も聞かれたことを知っています。でも、ここから抜け出したい。以下は私のコードです。
request.format.json? ?
"#{render :json => {:success => false, :message => 'Exception, provider code not found'}}" :
"#{raise 'Exception, provider code not found'}" if params[:provider_code].nil? || params[:provider_code].empty? || params[:provider_code].blank?
# Setting params for patient details.
conditions = {}
conditions['providers.provider_code'] = params[:provider_code] unless params[:provider_code].blank?
# Using scope to fetch record based on applied conditions.
patients = Patient.with_messages_provider.where(conditions)
# Response for patient details.
patients = Patient.patients_for_provider(patients)
render :json => patients