简体   繁体   中英

Filtered Search in Rails 4 using ActiveRecord Querying Interface

I have some filters in place when performing a search. I have another search filter that I am trying to implement, but can't seem to get it to work. I am having issues with the first_name last_name and building the view for that.

Controller:

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

Views:

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

You can try this

key = "%#{search_text}%"

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

PS: Not tried this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM