Active Record Associations — Ruby on Rails Guides
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. Quick & Dirty Auditing for Rails. inevitably hit a problem where everyone wished there had been a trail of what had been done. class Audit belongs_to:auditable, polymorphic: true belongs_to:user. PaperTrail lets you track changes to your Rails app's models' data. . What will happen when a parent (who has a paper trail) has_many children? parent_id }, like a polymorphic foreign key though I'm not sure whether that would cover.
The first 2 models, Business and Person, each can have interactions associated with them.
- Single-table inheritance vs. polymorphic associations in Rails: find what works for you
- A Paper Trail For Your Models
- Understanding Polymorphic Associations in Rails
These interactions share the same business logic regardless of the associated model so we can easily make interactions polymorphic. Rails Application Setup In order to utilize polymorphic associations in this tutorial, we will first need to create a few models. The first model, Person, will store information on people.
The second model, business, will store business information. The third model, Interaction, will store simple descriptions of various phone calls and other interactions that the user has had. Lets generate these models now. Run the commands listed below to create these models. First, open up your Person model and modify it so that it looks like the following code.
Open up your business model and modify it so that it looks like the following code.
Polymorphic Associations in Rails
Now open up the interaction model and verify that it looks like the following code. Rails should have already inserted the necessary line, but it doesn't hurt to double check. Together these two columns tell rails what model this interaction should be associated with. Now that we are finished with the models, it's time to create the controllers. We will create three controllers. The first controller, People, will allow us to create, edit, and update people.
The second controller, Businesses, will allow us to create, edit, and update businesses. The final controller, Interactions, will handle the saving of each interaction.
Run the commands below to create these controllers now.
Open up your routes file and add the following code, making sure not to overwrite your own application name with the example application name listed below. This allows us to easily create interactions for both people and businesses.
A Paper Trail For Your Models • AirBlade Software
Now lets add some code to each controller to handle reads, writes, and deletions. First open your people controller and modify it so that it looks like the code listed below.
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?
For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier.
Survey Says: Has One Polymorphic Nested
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.
For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared: From an instance of the Employee model, you can retrieve a collection of pictures: Similarly, you can retrieve product. If you have an instance of the Picture model, you can get to its parent via picture. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface: 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: