views:

30

answers:

1

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

+1  A: 

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!

Geoff Lanotte
wow!! That was fast.. Thanks a lot. I am installing it right now.. Seems right on spot. :D
Magician