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