我有一个应用程序,允许用户输入,编辑和搜索数据库中的项目。
在我的搜索功能中,您可以通过在搜索中添加参数来进行复杂的搜索。
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 %>
说明
如果我是管理员,并且未选中存档复选框,我希望查看所有未存档的项目。如果我检查一下,我只想查看存档的那些,包含在我搜索的其他参数中。
如果我是普通用户,我应该无法看到存档复选框,我所做的每一次搜索都应该在幕后虚假的项目上进行。
答案 0 :(得分:2)
如果archive
是一个布尔值,并且你想在其上搜索true或false,并且search_archive
也是一个布尔值,那么这就是你需要做的:
if search_archive.present?
_projects = _projects.where(:archive => search_archive)
end
更新:
您的复选框语法错误,需要类似于:
check_box("project", "archive", {:class => 'archive'})
答案 1 :(得分:2)
如果archive
是布尔值,请添加类似下面的内容
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 %>