Ruby On Rails multiple composite primary keys question

久未见 提交于 2019-12-08 04:42:59

问题


I am a new guy in Ruby, and I have tables with these primary keys:

  1. transaction_types:
    • transaction_type
  2. transaction_headers:
    • transaction_type
    • transaction_year
    • transaction_id
  3. transaction_details:
    • transaction_type
    • transaction_year
    • transaction_id
    • city_id
    • ticker_id
  4. tickers:
    • city_id
    • ticker_id

Of course, those models have other non primary keys such as customer_id, connection_id or date, or user_id, etc, but those are not important for relationships, as those are merely data or I don't have any problem with those.

These are my models:

#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

I need to perform a relationship to each correspond primary keys.. It was easy for transaction_type to transaction_detail and transaction_header, but how do I create an association between transaction_header and transaction_detail, and also between transaction_detail and ticker? How to create the :through keys for tickers relationships?

Thank you


回答1:


ActiveRecord does not support composite primary keys out of the box, but this plugin should going:

http://compositekeys.rubyforge.org/

They have a nice guide on how to get started.

Hope this helps!



来源:https://stackoverflow.com/questions/3220000/ruby-on-rails-multiple-composite-primary-keys-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!