I am writing a fairly simple web interface for a database of accounts / clients / contracts. I would like to have the following structure for my models:
class Invoice < ActiveRecord::Base
set_table_name 't10_invoices'
set_primary_key 'id_invoice'
has_one :client
end
class Client < ActiveRecord::Base
set_table_name 't20_clients'
set_primary_key 'id_client'
has_many :invoices
end
The problem is that I have the following database structure:
Tables:
t10_invoices:
id_invoice - primary_key
id_contract - foreign_key
t12_contracts:
id_contract - primary_key
t15_contracts_clients
id_client - foreign_key
id_contract - foreign_key
t20_clients
id_client - primary_key
There is a 1-1 relationship between t20_clientsand t12_contractsthrough t15_contracts_clients
and there is a 1-n relationship between t20_clientsand t10_invoices.
What is the difference between :foreign_keyand :association_foreign_key?
The problem is that I can’t change the structure of the database, and I completely lost it when redefining table names, foreign keys, join_foreign_keys, etc .... I would like an easy way to find invoice customers in normal mode Rails Invoice.find(:first).client
I would be grateful for any suggestions and help.