1

非標準スキーマでテーブル間の結合を機能させるのに苦労しています。それを行う適切な方法は、スキーマを移行して ActiveRecord 規則を使用することであることは知っていますが、これを使用してテスト目的でデータを消費する限り、それはオプションではありません。

結合は、両方のテーブルに存在するキー「AGREEMENT_TYPE_ID」を使用して行うことができます。

モデル定義には次のものがあります。

class Agreements < ActiveRecord::Base
    self.table_name = 'AGREEMENTS'
    self.primary_key = 'AGREEMENT_ID'
    has_one :Agreement_Types, :foreign_key => 'AGREEMENT_TYPE_ID'
end

class Agreement_Types < ActiveRecord::Base
    belongs_to :Agreements
    self.table_name = 'AGREEMENT_TYPES'
    self.primary_key = 'AGREEMENT_TYPE_ID'
end

これはインスタンス化です:

puts Agreements.joins(:Agreement_Types)

これは出力です:

C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/inheritance.rb:111:in     `compute_type': uninitialized constant Agreements
::AgreementTypes (NameError)
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/reflection.rb:172:in `klass'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency/join_association.rb:40:in `initialize'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:152:in `new'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:152:in `build_join_association'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:115:in `build'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:123:in `block in build'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:122:in `each'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:122:in `build'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/associations/join_dependency.rb:18:in `initialize'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:358:in `new'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:358:in `build_joins'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:266:in `build_arel'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation/query_methods.rb:260:in `arel'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation.rb:171:in `exec_queries'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation.rb:160:in `block in to_a'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/explain.rb:25:in `logging_query_plan'
        from C:/tools/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.7/lib/active_record/relation.rb:159:in `to_a'
        from C:in `to_ary'
        from prueba.rb:29:in `puts'
        from prueba.rb:29:in `puts'
        from prueba.rb:29:in `<main>'
4

1 に答える 1

0

私はうまくいく解決策を見つけます:

class AgreementType < ActiveRecord::Base
    self.table_name = 'AGREEMENT_TYPES'
    self.primary_key = 'AGREEMENT_TYPE_ID'
    belongs_to :Agreement
end

class Agreement < ActiveRecord::Base
    self.table_name = 'AGREEMENTS'
    self.primary_key = 'AGREEMENT_ID'
    has_one :AgreementType, :primary_key => 'AGREEMENT_TYPE_ID', :foreign_key => 'AGREEMENT_TYPE_ID'
end 
于 2012-08-06T10:38:05.677 に答える