Ruby on Rails的多元复合主键的问题 [英] Ruby On Rails multiple composite primary keys question
问题描述
我在Ruby的一个新的家伙,我与这些主键的表:
I am a new guy in Ruby, and I have tables with these primary keys:
- transaction_types:
- transaction_type
- transaction_types:
- transaction_type
- transaction_type
- transaction_year
- 的transaction_id
- transaction_type
- transaction_year
- 的transaction_id
- city_id
- ticker_id
- city_id
- ticker_id
当然,这些模型具有其他非主键如CUSTOMER_ID,CONNECTION_ID或日期,或USER_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.
这些都是我的模型:
#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
我需要执行一个关系到每个对应的主键。它是容易的 transaction_type 为 transaction_detail 和 transaction_header ,但怎么办我创造之间的关联 transaction_header 和 transaction_detail ,也与 transaction_detail 和股票?如何创建的:通过对行情关系
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?
感谢您
推荐答案
ActiveRecord的不支持复合主键开箱即用,但这个插件应该去:
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.
希望这有助于!
这篇关于Ruby on Rails的多元复合主键的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!