title: Ruby on Rails: Allowing separate Calendars to share the same Event?
body: <p>I'm having some trouble with the theory (and code) behind my events creator. A quick overview:
  I have <code>Customers</code> (the users) who each own a <code>Calendar</code>. A <code>Calendar</code> <code>belongs_to</code> a <code>Customer</code> Each <code>Calendar</code> <code>has_many</code> <code>Events</code>, and an <code>Event belongs_to</code> a <code>Calendar</code>:</p>
  
  <pre><code>#customer.rb
  class Customer &lt; ActiveRecord::Base
    belongs_to :business
    has_one :calendar, :dependent =&gt; :destroy
  ...
  end 
  
  #calendar.rb
  class Calendar &lt; ActiveRecord::Base
    belongs_to :customer
    has_many :events, :dependent =&gt; :destroy    
  end
  
  #event.rb
  class Event &lt; ActiveRecord::Base
    belongs_to :calendar
  
  end
  </code></pre>
  
  <p>When a Customer fills in a form in <code>events\new.html.erb</code> the Events_controller's <code>create</code> action picks up the info from the form, including an array <code>id_array</code> of each participant Customer that the creator of the event wished to be involved. </p>
  
  <p>So far, I have made it so that the events_controller iterates through the array and creates a corresponding <code>Event</code> for each of the participating customer's <code>Calendar</code>:</p>
  
  <pre><code>def create
  
      @calendar = current_customer.calendar
      @newevent = @calendar.events.build(event_params) #creates event in creator's calendar
  
      @participant_ids = params[:id_array]
  
      @participant_ids.each do |item| #iterates through id array, finds corresponding customer and creates event
        @participant = Customer.find(item)
        @part_calendar = @participant.calendar 
        @part_event = @part_calendar.events.build(event_params) #adds event to participant's calendar
      end
  
      if @newevent.save
        redirect_to '/main' #'/main/#{@calendar.id}'
      else
        redirect_to '/compose'
        end
  
    end
  </code></pre>
  
  <p>However, this is clearly not satisfactory, because none of these events created are <strong>connected to each other</strong> at all. I would like to know what the best most efficient and rubyish way is to allow all these Customer's Calendars to <strong>share</strong> the event (or share a unique identifier). This is so that if the creator decides to delete the event, it will be deleted from all participating calendars.</p>
  
  <pre><code>Here is also my event db migrate file for reference:
  
  class CreateEvents &lt; ActiveRecord::Migration
    def change
      create_table :events do |t|
  
        t.timestamps
        t.references :calendar, foreign_key: true
  
        t.string :name
        t.string :description
        t.date :day
        t.datetime :starts_at
        t.datetime :ends_at
        t.string :location
  
      end
    end
  end
  </code></pre>