Skip to content

Instantly share code, notes, and snippets.

@zoras
Created January 13, 2011 10:47
Show Gist options
  • Save zoras/777691 to your computer and use it in GitHub Desktop.
Save zoras/777691 to your computer and use it in GitHub Desktop.
#!ruby
# w = Station.first.build_worker(:reward => 2.0, :number => 1)
# w.save!
# Each station worker is copied to human_worker which is inherited from dummy_worker (later is referenced in station)
Station.all.each do |station|
worker = station.worker
human_worker = HumanWorker.new(:number => worker.number, :reward => worker.reward, :worker_type => worker.worker_type, :station_id => station.id)
station.dummy_worker = human_worker
human_worker.save!
station.worker.delete
station.save!
end
# same as worker.rb except class name
class DummyWorker
include Mongoid::Document
# Fields
field :worker_type
field :reward, :type => Float
field :number, :type => Integer, :default => 1
# Associations
referenced_in :station
# validates :reward, :human_reward => true
end
# human_woker inherits from dummy_worker which is referenced_in station
class HumanWorker < DummyWorker
include Mongoid::Document
validates :reward, :format => {:with => /^[0-9]*\.?[0-9]{1,2}$/, :message => "must be numeric with two digit at most after decimal"}
end
# added referenced dummy_worker
class Station
include Mongoid::Document
include Mongoid::Timestamps
# Fields
field :station_type
# Associations
embeds_one :instruction
# RobotWorker which inherits from worker is referenced_in Robot so we need reference worker instead of embedding it.
embeds_one :worker
# add referenced dummy_worker to copy embedded worker
references_one :dummy_worker
references_many :station_instances
referenced_in :line
# Validations
validates :station_type, :presence => true, :on => :create
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment