我有一個rails應用程序,允許用戶搜索數據庫中的項目。Ruby on Rails:搜索時出現重複結果
這裏是在項目模型中的搜索功能:
def self.search(search_industry, search_role, search_techs_ids)
_projects = Project.scoped
if search_industry.present?
_projects = _projects.where ['industry LIKE ?', like(search_industry)]
end
if search_role.present?
_projects = _projects.where ['role LIKE ?', like(search_role)]
end
if search_techs_ids.present?
_projects = _projects.joins(:technols).where("technols.id" => search_techs_ids)
end
_projects
end
,這裏是我的搜索頁面的一部分
<div class="tech">
<%= fields_for(@project_technol) do |ab| %>
Technologies :
<% tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil? %>
<%if params[:technols].nil?%>
<%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true}) %>
<% else %>
<%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true, :selected => tech_ids }) %>
<% end %>
</div>
這裏是我的搜索行動:
def search
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
@search = params[:industry], params[:role], tech_ids
@project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)
@search_performed = [email protected]! { |c| c.blank? }.empty?
@project = Project.new(params[:project])
@all_technols = Technol.all
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # search.html.erb
format.json { render :json => @project }
end
end
我問題是,當我在搜索頁面上選擇collection_select中的所有技術並單擊搜索時,結果會顯示毫秒顯示所有記錄重複,如果他們擁有多種技術。
例如,數據庫中有3個項目。 P1,P2,P3,都有3種技術,T1,T2和T3。如果我要搜索所有3種技術,每個項目應該顯示一次,但是它們顯示3次。
有沒有人有任何想法,我可能會出錯。提前致謝。
編輯
我PARAMS搜索時:
Parameters: {"utf8"=>"✓", "client"=>"", "industry"=>"", "technols"=>{"id"=>["", "40", "41", "42", "43", "44", "46", "47", "48", "49", "50", "51", "52", "53", "54"]}, "start_date_dd"=>"", "start_date_A"=>"", "start_date_B"=>"", "status"=>"", "per_page"=>"10"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Technol Load (0.3ms) SELECT "technols".* FROM "technols"
Project Load (0.8ms) SELECT "projects".* FROM "projects" ORDER BY client
Project Load (0.4ms) SELECT "projects".* FROM "projects" ORDER BY industry
Project Load (0.4ms) SELECT "projects".* FROM "projects" ORDER BY status
(1.0ms) SELECT DISTINCT COUNT(*) FROM "projects" INNER JOIN "projecttechnols" ON "projecttechnols"."project_id" = "projects"."id" INNER JOIN "technols" ON "technols"."id" = "projecttechnols"."technol_id" WHERE "technols"."id" IN (40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54)
Project Load (13.2ms) SELECT DISTINCT "projects".* FROM "projects" INNER JOIN "projecttechnols" ON "projecttechnols"."project_id" = "projects"."id" INNER JOIN "technols" ON "technols"."id" = "projecttechnols"."technol_id" WHERE "technols"."id" IN (40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54) ORDER BY project_name asc LIMIT 10 OFFSET 0
Technol Load (0.9ms) SELECT "technols".* FROM "technols" INNER JOIN "projecttechnols" ON "technols"."id" = "projecttechnols"."technol_id" WHERE "projecttechnols"."project_id" = 107
Technol Load (1.8ms) SELECT "technols".* FROM "technols" INNER JOIN "projecttechnols" ON "technols"."id" = "projecttechnols"."technol_id" WHERE "projecttechnols"."project_id" = 100
Technol Load (1.8ms) SELECT "technols".* FROM "technols" INNER JOIN "projecttechnols" ON "technols"."id" = "projecttechnols"."technol_id" WHERE "projecttechnols"."project_id" = 106
Rendered projects/search.html.erb within layouts/application (68.2ms)
Completed 200 OK in 100ms (Views: 74.8ms | ActiveRecord: 22.8ms)
UPDATE:
我剛剛發現了一個錯誤。
我的模型現在包含此代碼;
if search_techs_ids.present?
_projects = _projects.includes(:technols).where("technols.id" => search_techs_ids)
end
不顯示重複了,但我發現,當我搜索一個單一的技術,在顯示的結果表。我有此列:
<td><ul>
<% t.technols.each do |technol| %>
<li><%= technol.tech %><!/li>
<% end %>
</ul></td>
這用來顯示所有的技術,該項目的名單,但現在新的代碼,它只是顯示我搜索的一個。有任何想法嗎?
您正在使用的數據庫? – tommasop
Postgres數據庫 – Jazz
這就是爲什麼uniq過濾器不起作用,它在mysql中完美地工作,請參閱http:// stackoverflow。com/questions/9758793/rails-3-activerecord-postgresql -uniq-command-doesnt-work – tommasop