Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:45:47+00:00 2026-06-09T15:45:47+00:00

I am trying to make a search page where the user can search project

  • 0

I am trying to make a search page where the user can search project records on many different fields. I want them to be able to search on client, industry, start_date, end_date, and keywords.

At the moment, the page shows drop down menus for “Client” and “Industry”, and a text box for “Keywords”, so the user can select from the database and search on keywords.

I am having problems searching on a start date as I am trying to give the user the option to search for a “Start Date” in some time scale, i.e. 3 days ago, 1 week ago… in a drop down menu, OR search on specific dates by putting in a “Start Date” and “End Date” by using jquerys datepicker.

I am wanting it to take the specific dates as priority, incase the user selects both a time scale, and specific dates.

Here is my Search 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>    

Select start date:
<%= select_tag "start_date_dd", options_for_select({
    "Select a period" => "",
      "3 days ago"   => DateTime.now.to_date - 3.days,   # =    259_200 sec.
      "1 week ago"   => DateTime.now.to_date - 1.week,   # =    604_800 sec.
      "1 month ago"  => DateTime.now.to_date - 1.month,  # =  2_592_000 sec.
      "6 months ago" => DateTime.now.to_date - 6.months, # = 15_552_000 sec.
      "1 year ago"   => DateTime.now.to_date - 1.year,   # = 31_557_600 sec.
    }, :selected=>params[:start_date_dd] )%>    

or select project dates between

<%=  text_field_tag ("start_date") %>

and

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

Keywords :

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

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

<% end %>

Here is my project model that works without the specific dates option in search:

class Project < ActiveRecord::Base
  attr_accessible :client, :end_date, :industry, :keywords, :start_date, 


def self.search(search_client, search_industry, search_start_date_dd,  search_keywords) 
  return scoped unless search_client.present? || search_industry.present? || search_start_date_dd.present?  || search_keywords.present?

  where(['client LIKE ? AND industry LIKE ? AND DATE(start_date) BETWEEN ? AND ?  AND keywords LIKE ?', 
      "%#{search_client}%", "%#{search_industry}%" ,  
      search_start_date_dd, DateTime.now.to_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

Here is my search action from the project controller, which only takes into account the timescale search.

def search

@search = params[:client], params[:industry], params[:start_date_dd],  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

I had attempted to include the specific date search, but had no luck. Hopefully someone can point them in the right direction. I am a rails noob so please go easy on me. Thanks in advance.

EDIT: Here is my attempt at getting it to work.

def self.search(search_client, search_industry, search_start_date_dd, search_start_date, search_end_date, search_keywords) 
  return scoped unless search_client.present? || search_industry.present? || search_start_date_dd.present? || search_start_date.present? || search_end_date.present? || search_keywords.present?

where(['client LIKE ? AND industry LIKE ? AND DATE(start_date) BETWEEN ? AND ? OR DATE(start_date) BETWEEN ? AND ? AND DATE(end_date) BETWEEN ? AND ? AND keywords LIKE ?', 
      "%#{search_client}%", "%#{search_industry}%" , search_start_date_dd, DateTime.now.to_date, search_start_date, search_end_date, search_start_date, search_end_date,"%#{search_keywords}%"
    ])

end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:45:49+00:00Added an answer on June 9, 2026 at 3:45 pm

    I would modify the start_date_dd drop down to have a custom field, then add jQuery to show/hide the start_date/end_date fields. Then in your controller, you could look to see if start_date_dd was the custom field, then you should use the start_date/end_date fields.

    Other comments:

    I would not pass what they searched for directly to the database. I would first run something like the following:

    user_supplied_start_date = Time.zone.parse(search_start_date)
    

    That way if they give you an invalid date (parsed user_supplied_start_date == nil), you can give them a warning that you were unable to parse search_start_date and are showing them the last __ days instead.

    I’ve also seen the following code:

    def self.search
      scoped = scoped.where(['client like %?%', params[:client]) if params[:client].present?
      scoped = scoped.where(['client like %?%', params[:industry]) if params[:industry].present?
      ...
      scoped
    end
    

    That way you’re searching based on what they sent you.

    You only list the clients that are currently assigned to a project. Shouldn’t the user be able to search for all clients and see that _ client doesn’t have any projects?

    As a general rule, I prefer helper methods to having code in the view…

    Project.all.map {|p| [p.client]}.uniq
    

    vs

    # app/helpers/search_helper.rb
    def clients_for_select
      Client.select("id, name").map{|c| [c.id, c.name]}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to make a simple search inside a static HTML page using
new to c#. I'm trying to make a simple system where I can search
I'm trying to make a generic Search UserControl that can be given some values,
I want to make an AJAX call before the user leaves the page (so
I'm trying to make a search feature that will search multiple columns to find
I am trying to make a search function using the combination of DOM, PHP
im trying to make a simple search on my database from the index action
Afternoon, I am trying to make a simple search, which is working for the
I'm trying to make a Prolog predicate iscontained/2 : iscontained(List, Search) where it returns
I'm trying make a login window where a user is prompted to enter their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.