(sorry for any bad English)
Let's suppose I have models A, B, C. Each model have one address.
In the book "SQL Antipatterns: Avoiding the Pitfalls of Database Programming" (chapter 7 - Polymorphic Associations) there is a recipe to avoid such associations through the use of a "common super table" (also called base table or ancestor table).
Polymorphically, it would be:
table addresses:
id: integer
parent_type:string # 'A', 'B' or 'C'
parent_id: integer
I know you can use intersection tables, but the following solution looks more polished:
Instead of polymorphically associating A, B, C with Address, the recipe suggests to create a super table (Addressing) that have only a id field (surrogate key or pseudo key). Then, the other tables references Addressing. That way, says the author, "you can rely on the enforcement of your database’s data integrity by foreign keys". So, it would be:
table addressing
id: integer
table addresses
id: integer
addressing_id: integer (foreign_key)
zip: string
table a
id: integer
addressing_id: integer (foreign_key)
name: string
table b
id: integer
addressing_id: integer (foreign_key)
name: string
table c
id: integer
addressing_id: integer (foreign_key)
name: string
The SQL query would be like this:
SELECT * from a
JOIN address USING addressing_id
WHERE a.addressing_id = 1243
The QUESTION is: how to code such scenario in Rails ? I've tried in several ways without success.