Rails 3.1 - Events Listings - How to handle events that span many dates? -


i'm building events listing site. each event entered specific date , displayed in order......simple! however, need consider how handle events such festivals , plays span more 1 date. entering event on , on again each date isn't best option. i've thought have start date , end date, i'm not sure how make event show in index dates in between start/end. simple, thought i'd seek guidance more experience before potentially set off down wrong path this.

the event schema @ moment:

create_table "events", :force => true |t|     t.date     "event_date"     t.string   "headline"     t.text     "info"     t.integer  "event_type_id"     t.integer  "venue_id"     t.datetime "created_at"     t.datetime "updated_at"     t.string   "event_image_file_name"     t.string   "event_image_content_type"     t.integer  "event_image_file_size"     t.datetime "event_image_updated_at"     t.boolean  "free"   end 

and event controller index does:

 @events = event.paginate(:page => params[:page], :per_page => 12).find(:all, :conditions => ['event_date >= ?', date.today], :order => "event_date") 

edit: should have pointed out i'm listing events date header generated following line in event controllers index method:

@events_dates = @events.group_by { |e| e.event_date } 

the view along lines of:

<% @events_dates.sort.each |event_dates, events| %> <div class="well"> <h3> <% if event_dates == date.today %> today <% elsif event_dates == date.today + 1 %> tomorrow <% else %> <%= event_dates.to_date.to_formatted_s(:my_format) %> <% end %> </h3> </div>  <%= render events %> <% end %> 

the events partial has event headline, info etc.

so, think need alter way loop through date headers. instead of grouping event dates db , looping those, need loop through calendar days , see if events occur on dates based on event_start_date , event_end_date. ideas how approach that?

if have start , end date, using conditions hash:

:conditions => ["event_start_date <= ? , event_end_date >= ?", date.today, date.today] 

or arel:

.where("event_start_date <= ? , event_end_date >= ?", date.today, date.today) 

edit:

okay, want iterate on date range, , display events day. iterate on date range you'd use ruby's date.upto() function, , each date, iterate on events, , display events relevant current date.

the following code assuming you've set @start_date , @end_date in controller.

<% @start_date.upto(@end_date) |date| %> <div class="well"> <h3> <% if date == date.today %> today <% elsif date == date.today + 1 %> tomorrow <% else %> <%= date.to_formatted_s(:my_format) %> <% end %> </h3> </div>  <%= render @events.select{|event| event.start_date < date && event.end_date > date} %> <% end %> 

there's bit logic going on in view here though, , might have little rejigging work, should on right path.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -