0

執行搜索時,我有一些過濾器。我有另一個搜索過濾器,我試圖實現,但似乎無法讓它工作。我遇到了first_name last_name的問題,併爲此構建了視圖。使用ActiveRecord查詢界面在Rails 4中過濾搜索

控制器:

if params[:address_search] 
    @payment_requests = @payment_requests.where('full_address ILIKE ?', "%#{params[:address_search]}%") 

    unless params[:unit_search].blank? 
     @payment_requests = @payment_requests.where('unit ILIKE ?', "%#{params[:unit_search]}%") 
    end 
elsif params[:client_search] 
    @payment_requests = @payment_requests.where('email ILIKE ?', "%#{params[:client_search]}%") 
elsif params[:first_name] || params[:last_name] 
    @payment_requests = PaymentRequest.includes(:detail).where(payment_details: { first_name: params[:first_name], last_name: params[:last_name]}) 
    end 
else 
    @payment_requests = @payment_requests.active 
end 

瀏覽:

<div class="form-group"> 
    <%= text_field_tag 'client_search', '', id: 'pay-client-search', placeholder: 'Email Search', class: 'general-text-area pay-filter' %> 
</div> 

回答

1

你可以試試這個

key = "%#{search_text}%" 

@payment_requests = PaymentRequest.joins(:detail).where('payment_details.first_name LIKE :search OR payment_details.last_name LIKE :search', search: key) 

P.S:沒試過。

+0

鍵=「%#{search_text}%」進入視圖嗎? –

+0

編號它應該在控制器中。 search_text應該被您的變量名稱替換。例如:key =「%#{params [:first_name]}%」 – Jayaprakash