I have the following models:
Company, Orders, Invoices
Company has many Orders and also has many Invoices. Orders have one Invoice and belong to a company. Invoices belong to both order and company. Consequently, Orders reference company_id and Invoices reference company_id. I want to ensure invoice company_id is the same as its parent order's company_id.
Note: Postgres supports this. How to use rails to implement? https://www.postgresql.org/docs/9.3/ddl-constraints.html#DDL-CONSTRAINTS-FK
- Would I create a foreign key that ensures Invoice order_id + company_id exists in Orders as id + company_id?
- What's the best way to achieve this?
- Can this be done using migrations?
YAGNI
You're really overthinking and overcomplicating this. What you can do instead is get rid of the unessicary duplication caused by
invoices.company_idand just setup an indirect assocation:This avoids the whole issue altogether and all you need is the simple foreign keys created by the
references/belongs_tomigration macro.While what you're proposing could perhaps be done via composite foreign key there is no actual advantage to this approach.
The migrations DSL doesn't actually support it but you can always can run any arbitrary SQL. However the Ruby schema dumper will most likely not be able to reproduce it when parsing the schema so your foreign keys will be "lost in translation" when recreating the db from the schema unless you switch to SQL schema dumps.