Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Add note about inheretance and poly associations.

This note came about due to discussion in #6786.
  • Loading branch information...
commit e9acafd984ef64b44a8f0c998acaddbdce1dd6fb 1 parent f5654e7
@steveklabnik steveklabnik authored
Showing with 4 additions and 0 deletions.
  1. +4 −0 guides/source/association_basics.textile
4 guides/source/association_basics.textile
@@ -338,6 +338,10 @@ end
!images/polymorphic.png(Polymorphic Association Diagram)!
+Polymorphic associations can get tricky with inheritance. If you need
+an inheritance relationship in your ActiveRecord models, you should use
+Single Table Inheritance instead.
h4. Self Joins
In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as between manager and subordinates. This situation can be modeled with self-joining associations:

2 comments on commit e9acafd


Hi Steve,

I think the text should be as follows:

Polymorphic associations can get tricky when combined with Single Table Inheritance. For example, given the models Parent, Child (inheriting from Parent) and Polymorphic (with poly_type and poly_id used to reference other objects), if a new Polymorphic object was to point to a object of type Child, the "poly_type" field would contain "Parent" instead of "Child". Assuming the child object had an id of 1, when querying the Polymophic model, the query would have to be: Polymorphic.find_by_poly_type_and_poly_id("Parent", 1) as opposed to Polymorphic.find_by_poly_type_and_poly_id("Child", 1).

The benefit is that using "Parent" for poly_type allows you to query the polymorphic model for both child and parent objects instead of having to query individually for each child class.




Thanks Alex! Done in 1436751.

Please sign in to comment.
Something went wrong with that request. Please try again.