0

I'm building an events listing site. Currently each event is entered for a specific date and displayed in order......simple! However, I need to consider how to handle events such as festivals and plays that span more than one date. Entering the event over and over again for each date clearly isn't the best option. I've thought I could have a start date and end date, but i'm not sure how I would then make the event show in my index for the dates in between start/end. This is probably really simple, but I just thought i'd seek some guidance for those with more experience before I potentially set off down the wrong path with this.

The event schema at the moment:

create_table "events", :force => true do |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 my Event Controller index does:

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

EDIT: I should have pointed out that i'm listing the events with a date header generated by the following line in my Event Controllers Index method:

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

The the view is something along the lines of:

<% @events_dates.sort.each do |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 then has the event headline, info etc.

So, I think what I need to do is alter the way I loop through my date headers. Instead of grouping event dates from the db and then looping those, I almost need to loop through calendar days and see if events occur on those dates based on event_start_date and event_end_date. Any ideas how to approach that?

4

2 回答 2

1

If you have a start and end date, using the conditions hash:

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

Or arel:

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

Edit:

Okay, so you want to iterate over a date range, and then display any events for that day. To iterate over a date range you'd use ruby's Date.upto() function, and for each date, you could then just iterate over the events, and only display events relevant for the current date.

The following code is assuming you've set up the @start_date and @end_date in the controller.

<% @start_date.upto(@end_date) do |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 a bit too much logic going on in the view here though, and you might have to do a little rejigging to get it to work, but it should get you on the right path.

于 2012-06-09T08:38:55.177 回答
0

I don't think you need to group by.

@events = Event.where('start_date >= ? AND end_date >= ?', Date.today.beginning_of_day], Date.today.beginning_of_day]).order(:start_date)

In your view do something like:

  Today
  <% for event in @events %>
    <% if event.start_date == Date.today %>
      <%= render "events/event" %>
    <% end %>
  <% end 
  Tomorrow
  <% for event in @events %>
    <% if event.start_date == Date.tomorrow %>
      <%= render "events/event" %>
    <% end %>
  <% end %>

Is that what your trying to achieve?

You'll need to clean it up a bit, perhaps using a for event in @events.where date is today, but you get the idea

于 2012-06-09T12:21:22.930 回答