How do I find the range of dates in Ruby on Rails?

advertisements

I want to search data within the given date range. I have a database with having details of name, venue, start_at and end_at. I can search name and venue by defining find method in controller.following is my controller code.

def find
  @events=Event.find(:all, :conditions=>["venue = ? OR name = ?", params[:search_string], params[:search_string]])
end

Then it will find venue or name. But when I add start_at into this code it will return an exception. how can i search relevant date range from the database? plz anybody can help me? I am using postgresql and ruby 1.8.7 and rails 2.3.8 versions.

I changed my find method as follows...

def find
  if (params[:search_string])
    @events=Event.find(:all, :conditions=>["venue = ? OR name = ?", params[:search_string], params[:search_string]])
  else
    @events= Event.all(:conditions => ["start_at >= ? AND end_at <= ?",  params[:start_at], params[:end_at]])
  end
end

first if statement was properly working. but else part is not working. there is no exception. it will not retrieve date related data from database. I add this if statement into my code due to not retrieving data. can anybody help me to correct this second part? I enter date in my search textbox. but it will not retrieve any data.


Have a look at http://guides.rubyonrails.org/active_record_querying.html Try something like:

@events = Event.where("(venue = ? OR name = ?) AND start_at >= ? AND end_at <= ?", params[:search_string], params[:search_string], params[:start_at], params[:end_at])

Edit

In Rails 2.3.x, it should be

@events = Event.all(:conditions => ["(venue = ? OR name = ?) AND start_at >= ? AND end_at <= ?", params[:search_string], params[:search_string], params[:start_at], params[:end_at]])

Notice that the conditions is given as an array.