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:
- I have a calendar with Events.
- 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:
- If an event is not recurring, then it's basic event data is stored in the
Event
model (:name, :start_time, :end_time
). - If an event is recurring, then it's basic information (
:name, :start_time, :end_time
) is stored in theEvent
model, and then, all of it's following occurrences are stored in theEventOccurrence
model (:event_id, :date
). I omitted the (:start_time
and:end_time
) from theEventOccurrence
model, because I assumed this would be handled by its parent in theEvent
model.
Is the best way to store and retrieve events based on the scenario I provided above?