The complex is combined with obsolete tables in Rails

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.

+3
3

ContractClient Join, t15_contracts_clients. belongs_to :

class ContractClient < ActiveRecord::Base
  set_table_name 't15_contracts_clients'
  belongs_to :client, :foreign_key => :id_client
  belongs_to :contract, :foreign_key => :id_contract
end

Client:

class Client < ActiveRecord::Base
  set_table_name 't20_clients'
  set_primary_key 'id_client'
end

belongs_to :contract_client on Invoice, has_one :through, ContractClient:

class Invoice < ActiveRecord::Base
  set_table_name 't10_invoices'
  set_primary_key 'id_invoice'
  belongs_to :contract_client, :foreign_key => :id_contract
  has_one :client, :through => :contract_client
end

Invoice.find(:first).client , .

+2

. , has_one . , . , , . , -, , , , has_and_belongs_to_many. :association_foreign_key "" , :foreign_key , .

class Invoice < ActiveRecord::Base
   set_table_name 't10_invoices'
   set_primary_key 'id_invoice'
   belongs_to :contract, :foreign_key => 'id_contract'
   has_one :client, :through => :contract
end

class Contract
  set_table_name 't12_contracts'
  set_primary_key 'id_contract'
  has_and_belongs_to_many :clients, 
                           :join_table => 't15_contracts_clients' 
                           :foreign_key => 'id_contract',
                           :association_foreign_key => 'id_client'
  has_many :invoices, :foreign_key => 'id_contract'
end

class Client < ActiveRecord::Base
   set_table_name 't20_clients'
   set_primary_key 'id_client'
   has_and_belongs_to_many :clients, 
                           :join_table => 't15_contracts_clients' 
                           :foreign_key => 'id_client',
                           :association_foreign_key => 'id_contract'
   has_many :invoices, :through => :contracts
end

Invoice.find(:first).client
+2

Have you considered creating a view database ?

+1
source

Source: https://habr.com/ru/post/1725733/


All Articles