Ruby On Rails 多个复合主键问题

发布于 2024-09-09 02:28:34 字数 1569 浏览 5 评论 0原文

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

  1. transaction_types:
    • 交易类型
  2. transaction_headers:
    • 交易类型
    • transaction_year
    • transaction_id
  3. 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_typetransaction_detailtransaction_header 很容易,但是如何在 transaction_headertransaction_detail 之间以及 transaction_detailticker 之间创建关联?如何为股票关系创建 :through 键?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-09-16 02:28:34

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文