Sha256: 6a97a4ec2e4161d12b9a7a07870328bb348ed24a0b9862c532cc8542ff7f0122

Contents?: true

Size: 1.39 KB

Versions: 16

Compression:

Stored size: 1.39 KB

Contents

# assoc_test.rb

path = File.expand_path(File.join(File.basename(__FILE__), "..", "lib", "composite_primary_keys"))
puts path

require File.join(path)
require 'active_record'

$configuration = {
  :adapter  => 'postgresql',
  :database => 'cpk_test',
  :username => 'postgres'
}

ActiveRecord::Base.establish_connection($configuration) unless ActiveRecord::Base.connected?

module GlobePG
  class PGBase < ActiveRecord::Base
    self.abstract_class = true
   # establish_connection($configuration) unless connected?
  end
end

module GlobePG
  class TeacherToSchool < PGBase
    set_table_name 'teacher_to_school'
    self.primary_keys = ['teacherid', 'schoolid']

    belongs_to :globe_teacher, :foreign_key => 'teacherid'
    belongs_to :globe_school, :foreign_key => 'schoolid'
  end
end

module GlobePG
  class GlobeSchool < PGBase
    set_table_name 'globe_school'
    self.primary_key = 'schoolid'
    has_many :teacher_to_schools, :foreign_key => :schoolid
    has_many :globe_teachers, :through => :teacher_to_schools
  end
end

module GlobePG
  class GlobeTeacher < PGBase
    set_table_name 'globe_teacher'
    self.primary_key = 'teacherid'
    has_many :teacher_to_schools, :foreign_key => :teacherid
    has_many :globe_schools, :through => :teacher_to_schools
  end
end

teacher = GlobePG::GlobeTeacher.find_by_teacherid('ZZGLOBEY')
p teacher.globe_schools 

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
composite_primary_keys-8.1.8 test/db_test.rb
composite_primary_keys-12.0.0.rc5 test/db_test.rb
composite_primary_keys-12.0.0.rc4 test/db_test.rb
composite_primary_keys-12.0.0.rc3 test/db_test.rb
composite_primary_keys-12.0.0.rc2 test/db_test.rb
composite_primary_keys-12.0.0.rc1 test/db_test.rb
composite_primary_keys-8.1.7 test/db_test.rb
composite_primary_keys-11.2.0 test/db_test.rb
composite_primary_keys-11.1.0 test/db_test.rb
composite_primary_keys-11.0.3 test/db_test.rb
composite_primary_keys-11.0.2 test/db_test.rb
composite_primary_keys-10.0.5 test/db_test.rb
composite_primary_keys-9.0.10 test/db_test.rb
composite_primary_keys-11.0.1 test/db_test.rb
composite_primary_keys-11.0.0 test/db_test.rb
composite_primary_keys-10.0.4 test/db_test.rb