我正在努力使表之间的连接工作在非标准架构上。我知道正确的方法是迁移模式并使用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>'
答案 0 :(得分: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