Ruby on Rails:搜索后,列出不显示所有数据 [英] Ruby on Rails: After search, list not displaying all data

查看:220
本文介绍了Ruby on Rails:搜索后,列出不显示所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我在搜索中得到重复的结果,在我的rails应用程序,允许用户在数据库中搜索项目。



这是项目模型中的搜索功能:

  def self.search(search_industry,search_role,search_techs_ids)

_projects = Project.scoped

如果search_industry.present?
_projects = _projects.where ['industry LIKE?',like(search_industry)]
end
如果search_role.present?
_projects = _projects.where ['role LIKE?',like(search_role)]
end
如果search_techs_ids.present?
_projects = _projects.includes(:technols).where(technols.id=> search_techs_ids)
end
_projects
end

这是我的搜索页面的一部分

 < div class =tech> 
<%= fields_for(@project_technol)do | ab | %>
技术:
<%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 = !@search.reject! {| 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

排序,但在固定的时候,我注意到,当我搜索一个单一的技术,正确的项目显示在表中,但在技术列,它是为了显示属于该项目的所有技术,但它只显示一个我搜索:

 <%@ project_search.each do | t | %> 
< tr>
< td>< ul>
<%t.technols.each do | technol | %>
< li><%= technol.tech%><!/ li>
<%end%>
< / ul>< / td>

任何想法?在我的模型中,我过去有正确显示表的工作,但显示重复的结果。

  if search_techs_ids.present ? 
_projects = _projects.joins(:technols).where(technols.id=> search_techs_ids)
end

任何帮助将不胜感激。提前感谢



解决方案查看以下答案



@shioyama和@tommasop both帮助解决了这个问题。



解决方案是更改我的搜索视图,其中列是

 < td>< ul> 
<%t.technols(force_reload = true).each do | technol | %>
< li><%= technol.tech%>< / li>

<%end%>

< / ul>< / td>


解决方案

我遇到同样的问题, a href =http://stackoverflow.com/questions/9205039/matching-nested-model-association-attribute-with-includes>匹配嵌套模型关联属性与包含



另请参见关于globalize3 github问题页面的讨论,其中在尝试通过动态查找器搜索之后销毁记录(在数据库中留下不匹配的关联)时出现问题。



问题是你限制了连接到具有给定技术关联的项目(在 search_tech_ids 中具有 id 的项目),因此这些是rails返回的唯一行。 Rails无法知道有其他人。



基本上就我所知,最简单的解决方案只是在完成搜索后重新加载项目。在搜索方法的最后一行之上添加一行,如下所示:

  _projects.each {| p | p.technols.reload} 
_projects

这将告诉rails重新加载与每个项目,包括在搜索中不匹配的技术。



更新

尝试(见评论)和来自@tommasop的建议,解决方案是强制重新加载视图中的技术:

  t.technols(force_reload = true).each 

仍然不清楚为什么上面的重载方法不是加工。如果任何人有任何见解,请告诉我,我很想知道。


I had a problem where I was getting duplicate results in a search, in my rails app that allow the user to search for projects in a database.

Here is the search function in the project model:

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.includes(:technols).where("technols.id" => search_techs_ids)
    end
    _projects
    end

and here is part of my search page

<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>

Here is my search action:

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 = !@search.reject! { |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

That problem is now sorted, but in fixing that, I noticed that when I search for a single technology, the correct projects show up in the table, but in the technology column it is meant to show all the technologies belonging to that project but it only shows the one I searched for:

<% @project_search.each do |t| %>
  <tr>
    <td><ul>
      <% t.technols.each do |technol| %>
        <li><%= technol.tech %><!/li>
      <% end %>
    </ul></td>

Any ideas? In my model I used to have this code which worked for displaying the table correctly, but showed duplicate results.

if search_techs_ids.present?
        _projects = _projects.joins(:technols).where("technols.id" => search_techs_ids)
        end

Any help at all will be appreciated. Thanks in advance

SOLUTION see answers below

@shioyama and @tommasop both helped fix the problem.

The solution was to change my search view where the column is made to

<td><ul>
  <% t.technols(force_reload=true).each do |technol| %>
    <li><%= technol.tech %></li>

  <% end %>

</ul></td>

解决方案

I encountered the same problem in a different guise: Matching nested model association attribute with includes

See also this discussion on the globalize3 github issue page, where the problem came up when trying to destroy a record after searching by dynamic finder (leaving unmatched associations behind in the DB).

The problem is that you're restricting the join to projects with given technology associations (those with ids in search_tech_ids), so those are the only rows that rails returns. Rails has no way of knowing there are others there.

Basically so far as I can tell, the easiest solution is just to reload the projects after completing the search. Add a line just above the last line of your search method, like so:

_projects.each { |p| p.technols.reload }
_projects

This will tell rails to reload the technologies associated with each project, including the technologies it did not match in the search. I believe that should work in your case.

UPDATE:

After many attempts (see comments) and a suggestion from @tommasop, the solution was to force-reload the technologies in the view with:

t.technols(force_reload=true).each

Still not clear why the reload approach above wasn't working. If anyone has any insights please let me know, I'd be curious to hear.

这篇关于Ruby on Rails:搜索后,列出不显示所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆