ruby on rails - Can't get search on a date range based on created_at working -
i trying add ability search records based on from/to date range.
class search < activerecord::base has_many :documents # code below sets scopes based on search/input form entries. def search_documents documents = document.all documents = documents.where("subject ?", "%#{subject}%") if subject.present? documents = documents.where("body ?", "%#{text}%") if text.present? documents = documents.joins(:category).where("categories.name ?", "%#{category}%") if category.present? documents = documents.joins(:author).where("authors.name ?", "%#{author}%") if author.present? documents = documents.joins(:reviewer).where("reviewers.name ?", "%#{reviewer}%") if reviewer.present? documents = documents.where("created_at >= ?", "{from_date}") if from_date.present?
the first 5 lines work fine. problem last line date. way now, no records. i.e database has records created_at
date of 10/5/2015 through 10/21/2015, if enter 10/15/2015, no records though there records created after date.
from date comes form model. schema is:
t.date "from_date"
you need re write code this
documents = documents.where("created_at >= ?", from_date) if from_date.present?
Comments
Post a Comment