代码之家  ›  专栏  ›  技术社区  ›  Magician

rubyonrails多复合主键问题

  •  1
  • Magician  · 技术社区  · 15 年前

    我是Ruby的新手,我的表有以下主键:

      • 交易类型
      • 交易年度
    1. 交易详情:
      • 交易年度
      • 事务处理id
      • 城市id
    2. 股票代码:
      • 城市id
      • 股票代码

    当然,这些模型还有其他非主键,比如customer\u id、connection\u id或date、user\u id等等,但是这些对于关系来说并不重要,因为它们只是数据,或者我对它们没有任何问题。

    这些是我的模型:

    #models  
    
    class transaction_type < ActiveRecord::Base  
      has_many :transaction_headers, :foreign_key=>'transaction_type'  
      has_many :transaction_details, :foreign_key=>'transaction_type'
      has_many :tickers, :through=>:transaction_details
    end
    
    class transaction_header < ActiveRecord::Base  
      belongs_to: transaction_types, :foreign_key=>'transaction_type'
      has_many :transaction_details
      has_many :tickers, :through=>:transaction_details
    end
    
    class transaction_detail < ActiveRecord::Base
      belongs_to: transaction_headers
      has_many :tickers
    end
    
    class ticker < ActiveRecord::Base
    
    end
    

    交易类型 交易明细 ,但如何在 事务处理标题 ,以及 交易明细 股票行情 股票行情

    谢谢你

    1 回复  |  直到 15 年前
        1
  •  4
  •   Geoff Lanotte    15 年前

    ActiveRecord不支持开箱即用的复合主键,但该插件应该:

    http://compositekeys.rubyforge.org/

    他们有一本很好的入门指南。