2012-08-03 52 views
1

我很努力使表之間的聯接工作在非標準模式上。我知道這樣做的正確方法是遷移架構並使用ActiveRecord約定,但只要我使用它來爲測試目的使用數據,那不是一種選擇。Ruby:使用非標準模式加入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

嘗試運行'defined?協議' – 2012-08-03 11:30:41

+2

無論表格的非標準命名爲何,IMO都應該正確命名模型,即'Agreement'而不是'Agreements'和'AgreementType'而不是'Agreement_Types'。 – 2012-08-03 11:51:22

+0

謝謝@MladenJablanović。我認爲你是完全正確的,並會遵循你的建議。 – Tedi 2012-08-03 12:35:28

回答

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