Hello I get duplicate rows of data by inner join.and i write inner join with OR condition.
Controller userscontroller.rb
@users = User.search(params[:search])
@users = User.joins([:requests]).where("name LIKE ? OR destination LIKE ?","#{params[:search]}%", "#{params[:search]}%")
View searches/index.html.haml
%aside.span6
= form_tag(:users, method: "get") do
= text_field_tag "search", params[:search], placeholder: "Enter Name"
%br/
= submit_tag "Search", name: nil, class: "btn-custom-darken"
%br/
%br/
- @users.each do |user|
= render user
= will_paginate @users
View users/index.html.haml
%div{align: "center"}
%b List of Users
- if current_user != (@user)
%ol.microposts
- @users.each do |user|
= render user
The problem is duplicate data printed as a output after search. If user posted 3 request then user will be printed as output 3 times. So image of user is printed 3 times with duplicate request data. Thanks in advance.
To get unique values from a join just add the .uniq
function to the call. And also when reusing one single input for multiple queries you can use the ruby key:value pair syntax.
@users = User.joins([:requests])
.where("name LIKE :search OR
destination LIKE :search",
{search: params[:search]})
.uniq