0

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.

6
  • 1
    what exception? and how did you add start_at into this code?
    – fl00r
    Aug 11, 2011 at 11:20
  • start_at is date variable i add this as def find @events=Event.find(:all, :conditions=>["venue = ? OR name = ? OR start_at = ?", params[:search_string], params[:search_string],, params[:search_string]]) end
    – Rosh
    Aug 11, 2011 at 11:31
  • start_at in your example is a STRING not a DATE.
    – fl00r
    Aug 11, 2011 at 11:33
  • yeah start_at is a date....how can i change this?
    – Rosh
    Aug 12, 2011 at 3:06
  • Why do you mark an answer as accepted if the question is not answered yet? If people think your question is answered they will not look at it anymore.
    – Mischa
    Aug 12, 2011 at 8:37

2 Answers 2

0

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.

8
  • it is not working...again i got exception. undefined method `where' for #<Class:0x45a1b38>
    – Rosh
    Aug 11, 2011 at 11:28
  • How about posting that exception? :)
    – socjopata
    Aug 11, 2011 at 11:29
  • Yeah, @Octopus-Paul gave you Rails 3.x example
    – socjopata
    Aug 11, 2011 at 11:30
  • what class is Event? (It should be something like class Event < ActiveRecord::Base ...
    – cristian
    Aug 11, 2011 at 11:30
  • I changed this "where" to find then also got an exception.. "Couldn't find all Events with IDs (0,0) (found 0 results, but was looking for 2)"
    – Rosh
    Aug 11, 2011 at 11:37
0

I could find a way to search a data within given date range. I did this separately.I created two text_field_tag in my layout and created controller code as follow.

    def find
    @events= Event.all(:conditions => ["start_at >= ? AND end_at <= ?",  params[:search], params[:search_string]])

    end

Then I added following code in layout.

    <span style="text-align: right">
    <% form_tag "/events/find" do %>
    <%= text_field_tag :search%>
    <%= text_field_tag :search_string %>
    <%= submit_tag "search" %>
    <% end %>
    </span>

I got data from database.. Now I want to combine these two. finding a data by date range needs two text_field_tag and finding a data by name only need one text_field _tag. So how can I do this? need your help further more....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.