ruby on rails - Search form by start_time and end_time -
my models
class house < activerecord::base has_many: bookings end class booking < activerecord::base belongs_to: house end
the booking table has fields customer_name, start_time, end_time ect.
i want make search form (start_time, end_time) on houses page index page visitor can check wich houses available given period.
has ideas how start?
thanks..remco
in view, declare form non linked object
<% form_tag :url => search_book_path, :method => :get %> <%= text_field_tag :start_time %> <%= text_field_tag :end_time %> <%= submit_tag %> <% end %>
add route on books
map.resource :books, :collection => { search => :get }
in book_controller, declare function search:
def search start_time = params[:start_time] || datetime.now end_time = params[:end_time] || datetime.now @books = book.search(start_time, end_time) end
then declare search on book model
def search(start_time_, end_time_) return book.find(:all, :conditions => [ "#{book.table_name}.start_time >= ? , #{book.table_name}.end_time <= ?", start_time_, end_time_] ) end
not sure syntax, should give idea.
edit: didn't understand problem. model function search
should search available houses that:
- are not used @ required begin date : book.end_time < start_time_
- are not booked @ required end date : book.start_time > end_time_
like this
def search_for_availability_between(start_time_, end_time_) return book.find(:all, :conditions => [ "#{book.table_name}.start_time > ? , #{book.table_name}.end_time < ?", end_time_, start_time_] ) end
you should check tests, think should work.
Comments
Post a Comment