开发者

Ruby On Rails multiple composite primary keys question

开发者 https://www.devze.com 2023-01-06 12:48 出处:网络
I am a new guy in Ruby, and I have tables with these primary keys: transaction_types: transaction_type transaction_headers:

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_deta开发者_Python百科il and ticker? How to create the :through keys for tickers relationships?

Thank you


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消