public
Created

  • Download Gist
bug_1455.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
require 'active_record'
require 'logger'
 
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
 
# SCHEMA:
 
ActiveRecord::Schema.define do
# Unit, Person, Position
create_table "business_objects", :id => false, :force => true do |t|
t.string "id", :null => false
t.string "type", :null => false
end
 
# Unit => Position
create_table "managements", :force => true do |t|
t.string "unit_id", :null => false
t.string "position_id", :null => false
end
 
 
# Position => Contract
create_table "contract_refers_to_positions", :force => true do |t|
t.string "contract_id", :null => false
t.string "position_id", :null => false
end
 
# Contract => Person
create_table "contractings", :force => true do |t|
t.string "contract_id", :null => false
t.string "person_id", :null => false
end
 
# Person properties
create_table "person_attributes", :force => true do |t|
t.string "person_id", :null => false
t.string "first_name", :null => false
t.string "last_name", :null => false
end
end
 
# MODELS:
 
class BusinessObject < ActiveRecord::Base
set_primary_key :id
end
 
class Unit < BusinessObject
has_many :managements, :foreign_key => :unit_id
has_many :managers, :through => :managements, :source => :position
 
has_many :managers_contracts, :through => :managers, :source => :contracts
has_many :managers_people, :through => :managers_contracts, :source => :people
has_many :managers_properties, :through => :managers_people, :source => :properties
end
 
class Position < BusinessObject
has_many :managements
has_many :managed_units, :through => :managements
 
has_many :contract_refers_to_position
has_many :contracts, :through => :contract_refers_to_position, :source => :contract
end
 
class Contract < BusinessObject
has_many :contractings
has_many :people, :through => :contractings
 
has_many :contract_refers_to_position
has_many :positions, :through => :contract_refers_to_position
end
 
class PersonAttribute < ActiveRecord::Base
belongs_to :person, :inverse_of => :properties
end
 
class Person < BusinessObject
has_many :contractings
has_many :contracts, :through => :contractings
 
has_many :properties, :class_name => "PersonAttribute"
end
 
class Management < ActiveRecord::Base
belongs_to :unit
belongs_to :position
end
 
class ContractRefersToPosition < ActiveRecord::Base
belongs_to :position
belongs_to :contract
end
 
class Contracting < ActiveRecord::Base
belongs_to :contract
belongs_to :person
end
 
# SEEDS:
 
u = Unit.new
u.id = "123"
u.save!
 
u.managers_properties.to_a

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.