1

我有一个允许用户在数据库中输入、编辑和搜索项目的应用程序。

在我的搜索功能中,您可以通过在搜索中添加参数来进行复杂的搜索。

def self.like(text); "%#{text}%"; end

  def self.search(search_archive, search_client, search_status, search_fullname)
    # start with a scoped query, to apply more scopes on it afterwards
    _projects = Project.scoped 
    # then, for each of the parameters, apply the scope only if present

    if search_archive.present?
      _projects = _projects.where ['archive LIKE ?', like(search_archive)] 
    end
    if search_client.present?
      _projects = _projects.where ['client LIKE ?', like(search_client)] 
    end

     if search_status.present?
      _projects = _projects.where ['status LIKE ?', like(search_status)]
    end

if search_fullname.present?
      _projects = _projects.where ['fullname LIKE ?', like(search_fullname)]
    end
    # now you have applied only the present scopes. return the result, and watch 
    # the query as it executes.

    _projects

  end

现在我最近添加了存档项目的选项,该选项作为布尔值进入项目表。

我知道我的搜索功能是错误的,因为我认为我无法按照我尝试过的方式处理布尔值。

if search_archive.present?
          _projects = _projects.where ['archive LIKE ?', like(search_archive)] 
        end

这是错误:

PG::Error: ERROR:  operator does not exist: boolean ~~ unknown
LINE 1: SELECT COUNT(*) FROM "projects"  WHERE (archive LIKE '%{"{:c...

有任何想法吗?提前致谢。

更新:

更改为以下建议,

if search_archive.present?
  _projects = _projects.where(:archive => search_archive)
end

我收到此错误:

PG::Error: ERROR:  missing FROM-clause entry for table "archive"
LINE 1: SELECT COUNT(*) FROM "projects"  WHERE "archive"."{:class=>"...
                                               ^
: SELECT COUNT(*) FROM "projects"  WHERE "archive"."{:class=>""archive""}" = '1'

这是我的搜索操作:

def search


    @search = params[:archive], params[:client], params[:status], params[:fullname]

    @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])


respond_to do |format|
      format.html # search.html.erb
      format.json { render :json => @project }
    end

end 

添加部分搜索视图

<%= form_tag search_path, method: :get do %>

<% if current_user.try(:admin?) %>
Archive:
<%= check_box :archive, :class => "archive" %></H2>
<% end %>
<% if !current_user.try(:admin?) %>
<%= hidden_field :archive, :value => false %>
<% end %>

<div class="client">
Client : 
<%= select(@projects, :client, Project.order("client").map{|p| [p.client]}.uniq, :prompt => "-Any-", :selected => params[:client]) %></br>
</div>

<div class="buttons">
<div id="search_button">
<%= submit_tag "Search", name: nil, :class => "button" %>
</div>

<% end %>

解释

如果我是管理员,并且未选中存档复选框,我希望查看所有未存档的项目。如果我检查它,我只想查看存档的,包含在我搜索的其他参数中。

如果我是普通用户,我应该看不到存档复选框,并且我所做的每次搜索都应该在幕后存档错误的项目上进行。

4

2 回答 2

2

ifarchive是一个布尔值,并且您想在其上搜索 true 或 false,并且search_archive它也是一个布尔值,这就是您需要做的所有事情:

if search_archive.present?
  _projects = _projects.where(:archive => search_archive)
end

更新:

您的复选框语法错误,需要类似于:

check_box("project", "archive", {:class => 'archive'})

于 2012-10-17T11:49:21.383 回答
2

if archiveis Boolean 添加如下内容

if search_archive.present?
  _projects = _projects.where(:archive => search_archive != "false")
end

改变

<% if current_user.try(:admin?) %>
Archive:
<%= check_box :archive, :class => "archive" %></H2>
<% end %>
<% if !current_user.try(:admin?) %>
<%= hidden_field :archive, :value => false %>
<% end %>

<% if current_user.try(:admin?) %>
  Archive: <%= check_box_tag :archive, true, false, :class => "archive" %></H2>
<% else %>
  <%= hidden_field :archive, :value => false %>
<% end %>
于 2012-10-17T12:03:25.727 回答