Ruby On Rails 多个复合主键问题
我是 Ruby 的新手,我有包含以下主键的表:
- transaction_types:
- 交易类型
- transaction_headers:
- 交易类型
- transaction_year
- transaction_id
- transaction_details:
- 交易类型
- transaction_year
- transaction_id
- city_id
- ticker_id
- :
- city_id
- ticker_id
当然,这些模型还有其他非主键,例如 customer_id、connection_id 或日期、或 user_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
我需要与每个对应的主键建立关系。transaction_type 到 transaction_detail 和 transaction_header 很容易,但是如何在 transaction_header 和 transaction_detail 之间以及 transaction_detail 和 ticker 之间创建关联?如何为股票关系创建 :through 键?
谢谢
I am a new guy in Ruby, and I have tables with these primary keys:
- transaction_types:
- transaction_type
- transaction_headers:
- transaction_type
- transaction_year
- transaction_id
- transaction_details:
- transaction_type
- transaction_year
- transaction_id
- city_id
- ticker_id
- 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ActiveRecord 不支持开箱即用的复合主键,但此插件应该:
http://compositekeys.rubyforge.org/
他们有一个很好的入门指南。
希望这有帮助!
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!