gpt4 book ai didi

ruby-on-rails - ruby rails : Show results between 2 dates

转载 作者:数据小太阳 更新时间:2023-10-29 08:33:23 24 4
gpt4 key购买 nike

我在搜索我的数据库时使用 jquery 日期选择器来选择两个日期。目前搜索只会搜索确切的开始日期和确切的结束日期,但我希望用户能够选择两个日期,他们可以在日期之间进行搜索。

这是我的模型:

class Project < ActiveRecord::Base
attr_accessible :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech

validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech





def self.search(search_client, search_industry, search_role, search_tech, search_business_div, search_project_owner, search_exception_pm, search_status, search_start_date, search_end_date, search_keywords)
return scoped unless search_client.present? || search_industry.present? || search_role.present? || search_tech.present? || search_business_div.present? || search_project_owner.present? || search_exception_pm.present? || search_status.present? || search_start_date.present? || search_end_date.present? || search_keywords.present?

if search_start_date != ""
search_start_date = Date.parse(search_start_date).strftime("%Y-%m-%d")
end
if search_end_date != ""
search_end_date = Date.parse(search_end_date).strftime("%Y-%m-%d")
end



where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND start_date LIKE ? AND end_date LIKE ? AND keywords LIKE ?', "%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" , "%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%", "%#{search_start_date}%", "%#{search_end_date}%","%#{search_keywords}%"])



end


def self.paginated_for_index(projects_per_page, current_page)
paginate(:per_page => projects_per_page, :page => current_page)
end

end

这是我的搜索 View :

<h1>Search</h1>

<% if @project_search.total_entries > 0 %>
<%= form_tag search_path, method: :get do %>

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

Industry :
<%= select(@projects, :industry, Project.all.map {|p| [p.industry]}.uniq, :prompt => "-Any-", :selected => params[:industry]) %></br>

Role :
<%= select(@projects, :role, Project.all.map {|p| [p.role]}.uniq, :prompt => "-Any-", :selected => params[:role]) %></br>

Technologies :
<%= select(@projects, :tech, Project.all.map {|p| [p.tech]}.uniq, :prompt => "-Any-", :selected => params[:tech]) %></br>

Business Division :

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

Project Owner :
<%= select(@projects, :project_owner, Project.all.map {|p| [p.project_owner]}.uniq, :prompt => "-Any-", :selected => params[:project_owner]) %></br>

Exception PM
<%= select(@projects, :exception_pm, Project.all.map {|p| [p.exception_pm]}.uniq, :prompt => "-Any-", :selected => params[:exception_pm]) %></br>


Start Date :

<%= text_field_tag("start_date") %></br>


End Date :

<%= text_field_tag("end_date") %></br>


Status :

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

Keywords :

<%= text_field_tag :keywords, params[:keywords] %></br>

<%= submit_tag "Search", name: nil %>

<% end %>


<% if @search_performed %>
<h3><%=@project_search.total_entries%> results</h3>



<table class = "pretty">
<table border="1">
<tr>
<th><%= sortable "project_name", "Project name" %> </th>
<th><%= sortable "client", "Client" %></th>
<% if false %>
<th>Exception pm</th>
<th>Project owner</th>
<% end %>
<th><%= sortable "tech", "Technologies" %></th>
<th><%= sortable "role", "Role" %></th>
<th><%= sortable "industry", "Industry" %></th>
<% if false %>
<th>Financials</th>
<th>Business div</th>
<th>Status</th>
<th>Start date</th>
<th>End date</th>
<th>Entry date</th>
<th>Edited date</th>
<th>Summary</th>
<th>Lessons learned</tStackh>
<th>Customer benifits</th>
<th>Keywords</th>
<!th></th>
<!th></th>
<!th></th>
<% end %>
</tr>



<% @project_search.each do |t| %>
<tr>
<td><%= t.project_name %></td>
<td><%= t.client %></td>
<% if false %>
<td><%= t.exception_pm %></td>
<td><%= t.project_owner %></td>
<% end %>
<td><%= t.tech %></td>
<td><%= t.role %></td>
<td><%= t.industry %></td>
<% if false %>
<td><%= t.financials %></td>
<td><%= t.business_div %></td>
<td><%= t.status %></td>
<td><%= l(t.start_date) if t.start_date? %></td>
<td><%= l(t.end_date) if t.end_date? %></td>
<td><%= t.entry_date %></td>
<td><%= t.edited_date %></td>
<td><%= t.summary %></td>
<td><%= t.lessons_learned %></td>
<td><%= t.customer_benifits %></td>
<td><%= t.keywords %></td>
<% end %>
<td><%= link_to 'Show', t %></td>
<!td><%#= link_to 'Edit', edit_project_path(project) %></td>
<!td><%#= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<%end %>

<% if @search_performed %>
Results per page: <%= select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i), :onchange => "if(this.value){window.location='?per_page='+this.value;}" %>

<% end %>

<% else %>
<h2> Sorry, there are no results matching your search. Please try again. </h2>
<% end %>
<br />
<%# end %>

<% if @search_performed %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= hidden_field_tag :per_page, params[:per_page] %>
<%= hidden_field_tag :page, params[:page] %>

<%= will_paginate (@project_search) %>


<%= button_to "Search Again?", search_path, :method => "get" %>

<% end %>
<%= button_to "Home", projects_path, :method => "get" %>

这是我的 Controller 的一部分:

def search

@search = params[:client], params[:industry], params[:role], params[:tech], params[:business_div], params[:project_owner], params[:exception_pm], params[:status], params[:start_date], params[:end_date], params[:keywords]

@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

希望有人能帮助我。我是 Rails 的新手,所以放轻松 :)。提前致谢。

最佳答案

我假设您想显示所有在 search_start_date 之间运行的项目和 search_end_date .所以这将转化为以下逻辑:

  • 项目开始时间:

    project.start_date BETWEEN search_start_date AND search_end_date

  • 项目在时间范围内结束:

    project.end_date BETWEEN search_start_date AND search_end_date

  • 项目在时间范围之前开始并在时间范围之后结束:

    project.start_date <= search_start_date AND project.end_date => search_end_date

您可以直接将其转换为 SQL 并将其放入您的 where 命令。

作为一个建议:我会将您的 where 命令中的问号更改为命名占位符,因为它使您的查询更具可读性。 ActiveRecord 查询指南包含详细信息:http://guides.rubyonrails.org/active_record_querying.html#conditions (第 2.2.1 节占位符条件)

关于ruby-on-rails - ruby rails : Show results between 2 dates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11756393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com