From 52901e976c19e1e581ced111d67776cdeb3185d7 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Fri, 14 Aug 2015 23:33:08 +0300 Subject: [PATCH] Add conference_id to Event --- app/models/event.rb | 2 +- ...50814202053_add_conference_id_to_events.rb | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20150814202053_add_conference_id_to_events.rb diff --git a/app/models/event.rb b/app/models/event.rb index 3206c86..9034fc8 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -2,7 +2,7 @@ class Event < ActiveRecord::Base include Proposable has_one :track, through: :proposition, source: :proposition_accepting, source_type: Track - has_one :conference, through: :track + belongs_to :conference has_many :participations has_many :pending_participations, ->() { pending }, class_name: 'Participation' diff --git a/db/migrate/20150814202053_add_conference_id_to_events.rb b/db/migrate/20150814202053_add_conference_id_to_events.rb new file mode 100644 index 0000000..fa2d097 --- /dev/null +++ b/db/migrate/20150814202053_add_conference_id_to_events.rb @@ -0,0 +1,25 @@ +class Event < ActiveRecord::Base + has_one :proposition, as: :proposable +end + +class Proposition < ActiveRecord::Base + belongs_to :proposable, polymorphic: true + belongs_to :proposition_accepting, polymorphic: true +end + +class Track < ActiveRecord::Base +end + +class AddConferenceIdToEvents < ActiveRecord::Migration + def up + add_reference :events, :conference, index: true, foreign_key: true + + Event.all.includes(:proposition).each do |event| + event.update!(conference_id: event.proposition.proposition_accepting.conference_id) + end + end + + def down + remove_reference :events, :conference, index: true, foreign_key: true + end +end