From 1ea96bfdff6d84ad9d0e99374f4953609f7d383f Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Fri, 14 Aug 2015 23:40:32 +0300 Subject: [PATCH] Add track_id to Event --- app/models/event.rb | 2 +- app/models/track.rb | 2 +- .../20150814203341_add_track_id_to_events.rb | 21 +++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20150814203341_add_track_id_to_events.rb diff --git a/app/models/event.rb b/app/models/event.rb index 9034fc8..0992eac 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,8 +1,8 @@ class Event < ActiveRecord::Base include Proposable - has_one :track, through: :proposition, source: :proposition_accepting, source_type: Track belongs_to :conference + belongs_to :track has_many :participations has_many :pending_participations, ->() { pending }, class_name: 'Participation' diff --git a/app/models/track.rb b/app/models/track.rb index 3f4f010..a8bf6f1 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -2,7 +2,7 @@ class Track < ActiveRecord::Base include PropositionAccepting belongs_to :conference - has_many :events, through: :propositions, source: :proposable, source_type: Event + has_many :events validates :name, presence: true validates :color, presence: true, format: {with: /\A#?[a-f0-9]{6}\z/i} diff --git a/db/migrate/20150814203341_add_track_id_to_events.rb b/db/migrate/20150814203341_add_track_id_to_events.rb new file mode 100644 index 0000000..b47b334 --- /dev/null +++ b/db/migrate/20150814203341_add_track_id_to_events.rb @@ -0,0 +1,21 @@ +class Event < ActiveRecord::Base + has_one :proposition, as: :proposable +end + +class Proposition < ActiveRecord::Base + belongs_to :proposable, polymorphic: true +end + +class AddTrackIdToEvents < ActiveRecord::Migration + def up + add_reference :events, :track, index: true, foreign_key: true + + Event.all.includes(:proposition).each do |event| + event.update!(track_id: event.proposition.proposition_accepting_id) + end + end + + def down + remove_reference :events, :track, index: true, foreign_key: true + end +end