Polymorphic Associations in Rails - raznomir.info
A slightly more advanced twist on associations is the polymorphic association. With polymorphic associations, a. I have a problem with a polymorphic association and its type field. that something is not working well with a custom imageable_type and Paper Trail. Without the two has_paper_trail, Rails knows it is a custom type field. The code of interest is in the #index action and its query for trails: This is one of the easiest query problems to fix, using an eager load of the relation. and Rails will properly eager load, taking into account the polymorphic nature of the.
For example, in an application containing authors and books, the author model could be declared like this: This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.
For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this: For example, if you assign: If some that existed previously are now missing, then their join rows are automatically deleted.
Automatic deletion of join models is direct, no destroy callbacks are triggered. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way: This association indicates that the declaring model can be matched with one instance of another model by proceeding through a third model.
For example, if each supplier has one account, and each account is associated with one account history, then the supplier model could look like this: For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way: How do you know which is which?
Understanding Polymorphic Associations in Rails
For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this: In current versions of Rails, you can abstract away this implementation detail by using t.
Rails offers two different ways to declare a many-to-many relationship between models. This makes the association indirectly, through a join model: With polymorphic associations, a model can belong to more than one other model, on a single association.
Moving on to the implementation. First, let's assume that our domain already has Event, Restaurant and Review models. What we now want is for a review to be able to belong to either an event or a restaurant.
We need to set the kind of entity it belongs to. Since this can be either an event or a restaurant, we're going to need a more generic entity. Let's name it Reviewable. Had we opted for a superclass instead of a mixin, this entity would've been named something like "Attraction" or "Venue". The database representation of this polymorphism consists of two columns, which represent the ID and the type of the actual entity that our review will belong to.
Let's update the Review spec: At this point, all of our scenarios should fail. Let's proceed by altering the database schema. In order to do that, we'll need a migration which expands the reviews table and adds a reference to the reviewable entity. We'll run the following: If we run the Review specs again, the scenarios testing for the existance of columns should pass.
Let's fix that in the Review model: Running our Review spec again should result in all scenarios passing.Polymorphic Association In Rails
On that side, we'll abstract the association by creating a concern called Reviewable. A superclass for Event and Restaurant is another option here.
Active Record Associations — Ruby on Rails Guides
We'll start by writing a shared spec example for the concern: Let's now include this in the Event and Restaurant specs. Now, let's implement the actual logic.
First, we'll write the concern: