Ruby on Rails: Allowing separate Calendars to share the same Event? -
i'm having trouble theory (and code) behind events creator. quick overview: have customers
(the users) each own calendar
. calendar
belongs_to
customer
each calendar
has_many
events
, , event belongs_to
calendar
:
#customer.rb class customer < activerecord::base belongs_to :business has_one :calendar, :dependent => :destroy ... end #calendar.rb class calendar < activerecord::base belongs_to :customer has_many :events, :dependent => :destroy end #event.rb class event < activerecord::base belongs_to :calendar end
when customer fills in form in events\new.html.erb
events_controller's create
action picks info form, including array id_array
of each participant customer creator of event wished involved.
so far, have made events_controller iterates through array , creates corresponding event
each of participating customer's calendar
:
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 |item| #iterates through id array, finds corresponding customer , creates event @participant = customer.find(item) @part_calendar = @participant.calendar @part_event = @part_calendar.events.build(event_params) #adds event participant's calendar end if @newevent.save redirect_to '/main' #'/main/#{@calendar.id}' else redirect_to '/compose' end end
however, not satisfactory, because none of these events created connected each other @ all. know best efficient , rubyish way allow these customer's calendars share event (or share unique identifier). if creator decides delete event, deleted participating calendars.
here event db migrate file reference: class createevents < activerecord::migration def change create_table :events |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
you can have event
self referenced: event have many events , belongs event:
model
class event < activerecord::base has_many :events, dependent: :destroy belongs_to :event, class_name: "event" end
you have add event_id
migration integer
controller
def create @calendar = current_customer.calendar @newevent = @calendar.events.build(event_params) #creates event in creator's calendar if @newevent.save # wait until it's saved because need id @participant_ids = params[:id_array] @participant_ids.each |item| @participant = customer.find(item) @part_calendar = @participant.calendar # merge newly created event id params @part_event = @part_calendar.events. build(event_params.merge(event_id: @newevent.id)) @part_event.save end redirect_to '/main' #'/main/#{@calendar.id}' else redirect_to '/compose' end end
this way have this:
let's customer
has calendar , through form created event
, selected other customers part of event
:
the current_customer
have new event
, each of other customer
s selected have new event
( have shared parent event
new event
of current_customer
). way:
- the newly created
event
have childrenevents
viaevent.events
wiched - if destroy
event
ofcurrent_customer
, it's childrenevent
s destroy - you can access parent event of particular
event
viaevent.event
Comments
Post a Comment