【问题标题】:Ruby: Join with ActiveRecord using non-standard schemaRuby:使用非标准模式加入 ActiveRecord
【发布时间】:2012-08-03 11:23:30
【问题描述】:

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

【问题讨论】:

  • 尝试运行defined? Agreements
  • 不管你的表有什么非标准命名,IMO你应该正确命名你的模型,即Agreement而不是AgreementsAgreementType而不是Agreement_Types
  • 谢谢@MladenJablanović。我认为你是完全正确的,并且会听从你的建议。
  • @denis.peplin 它什么都不返回,但是当我执行 'puts Agreements.find(:first).agreement_id' 时它会正确返回内容

标签: ruby activerecord join


【解决方案1】:

我找到了一个可行的解决方案:

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 

【讨论】:

    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2020-06-11
    • 2019-10-18
    • 2011-12-27
    • 2010-11-19
    • 1970-01-01
    相关资源
    最近更新 更多