简体   繁体   中英

Search records between two dates - Ruby on Rails

I am trying to create a search that returns records between two dates (today and a future date).

I can get it to return several records no problem if I use the following code in my model (film.rb):

def self.date_search(search_string)
    self.where("release_date >= ?", search_string  )

However, when I try something like the following, I receive syntax errors:

    def self.date_search(search_string)
       date = Date.today
       self.where("release_date = created_at > date.strftime("%F") AND created_at < ?  ", search_string  )

I am still very new to Ruby so any help sorting out my syntax and code would be much appreciated.

Try:

def self.date_search(search_string)
  self.where({release_date: Time.now..search_string})
end

This will give you entries where release_date is between the current time and the search_string (inclusive of the search string since you use two dots(..), it would be exclusive of the search string if you used three dots (...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM