Tutorial :Ruby On Rails multiple composite primary keys question



Question:

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


Solution: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!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »