1

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?

Community
  • 1
  • 1
Dodinas
  • 6,705
  • 22
  • 76
  • 108
  • What structure of a json output you need to get ? Is there an examples? – itsnikolay Jan 31 '14 at 13:07
  • @itsnikolay, please see my edit in the question. – Dodinas Jan 31 '14 at 14:37
  • The issue is: you can not get `event_occurrences` in json? If it is then you could use `joins` `@events = Event.joins(:event_occurrences)` – itsnikolay Jan 31 '14 at 14:49
  • Hi @itsnikolay, so, what you did works. BUT, it only works if the parent `Event` has an `EventOccurrence` object. But, I also want to gather the `Event` objects that don't have an `EventOccurrence` as well. I'm completely confused now. I think it's an issue with my Models. I will do my best to articulation what I am trying to achieve in an edit above. – Dodinas Jan 31 '14 at 15:51

2 Answers2

2

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.

Dodinas
  • 6,705
  • 22
  • 76
  • 108
1
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
itsnikolay
  • 17,415
  • 4
  • 65
  • 64
  • Thanks for your response (and patience), in trying to figure this out. Based one what you gave me above, it basically only renders the `Event` objects and does not render any of the `EventOccurrences.` So, I'm still perplexed as to what is actually happening. Also, I did add one more piece of my `Event` model above where I define `as_json`, which I forgot to include. Maybe this will help? Thanks for sticking with me on this, as I am banging my head trying to figure it out!! – Dodinas Jan 31 '14 at 17:43
  • What if `@events = Event.includes(:event_occurrences)` other code stay the same, still no event_occurrences in json output ? – itsnikolay Jan 31 '14 at 18:35
  • So let's try to reproduce this issue on Github in a test project. If there's no better way. – itsnikolay Feb 01 '14 at 08:43