问题
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
回答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