Frage

I have the following schema for displaying events with FullCalendar:

class Event < ActiveRecord::Base
  has_many :event_occurrences, dependent: :destroy

  def as_json(options = {})
   {
  :id => self.id,
  :title => self.name,
  :start => start_time.rfc822,
  :end => end_time.rfc822,
  }
  end
end

class EventOccurence < ActiveRecord::Base
  belongs_to :event
end

The makeup of an Event object (I've removed some fields for brevity):

   => Event(id: integer, name: string, start_time: datetime, end_time: datetime)

The makeup of an EventOccurence object:

   => EventOccurrence(id: integer, event_id: integer, date: datetime) 

The JSON output would look like this:

 {:id=>1, :title=>"Your title", :location=>"Bermuda", :start=>"Sun, 05 Jan 2014 02:50:07 +0000", :end=>"Sun, 05 Jan 2014 02:51:46 +0000"}

The reason I have a separate EventOccurence model is to store recurring events separately, as was suggested in this StackOverflow post, as am I also trying to create a separate join table to store recurring events.

Currently, I am able to query only the Event objects fine for FullCalendar. However, the Associations or EventOccurrence objects are not showing up using this:

@events = Event.all.includes(:event_occurrences)

Here is what I am currently attempting:

calendar.js.erb

....
eventSources: [{
        url: '/calendar/index'
    }],
....

calendar_controller.erb

def index
  @events = Event.all.includes(:event_occurrences)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @events }
  end
end

Am I going about this the right way? Does my EventOccurrence model need any additional fields to it?

EDIT 1:

Let me do my best to state what I am trying to achieve:

  1. I have a calendar with Events.
  2. Some of these Events are "recurring" events.
    => With these recurring events, I would like to store them separately into a 'join' table, so that they can be filtered within the parameters of a given month when queried. (as suggested here).

My models are above, as Event and EventOccurrence. The way it currently works is:

  1. If an event is not recurring, then it's basic event data is stored in the Event model (:name, :start_time, :end_time).
  2. If an event is recurring, then it's basic information (:name, :start_time, :end_time) is stored in the Event model, and then, all of it's following occurrences are stored in the EventOccurrence model (:event_id, :date). I omitted the (:start_time and :end_time) from the EventOccurrence model, because I assumed this would be handled by its parent in the Event model.

Is the best way to store and retrieve events based on the scenario I provided above?

War es hilfreich?

Lösung

Okay, so I figured out what I need to do. With some changes to my models, I came up with:

 --- !ruby/object:Event
   attributes:
   id: 
   name: 
   details: 
   location: 
   etc...

And

 --- !ruby/object:EventOccurrence
   attributes:
   id: 
   event_id:  
   start_time: 
   end_time:

And then, for the JSON that FullCalendar likes:

def index
@events = Event.all
@hash = []
@events.each do |e|
    @occurrences = e.event_occurrences
    @occurrences.each do |occurrence|
        @hash << {
          :id => e.id,
          :title => e.name,
          :location => e.location,
          :details => e.details,
          :start => occurrence.start_time.rfc822,
          :end => occurrence.end_time.rfc822,
        }
    end
end

   respond_to do |format|
     format.html # index.html.erb
     format.json { render json: @hash }
   end
end

And this got all my events to display properly.

Andere Tipps

def index
  @events = Event.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @events.to_json(include: :event_occurrences) }
  end
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top