Full-Stack Web Developer
To understand Rails Polymorphic Association, let us refresh our minds on Rails Active Record. What is Rails Active Record?
Rails Active Record is the M in the MVC architecture framework. The model which is the layer of the system responsible for representing business logic and data. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
It is an implementation of the Active Record Pattern which itself is a description of an Object Relational Mapping System.
Polymorphic Association is an Active Record Association that can connect a model to multiple other models. For example; we can use a single association to connect a Post model with Comment and Like models, allowing us to query the Post model.
Let get on with the code, first, we create a rails app
rails new post_app
This command will generate the basic files we need. Now we will generate 3 models as seen in the diagram above. Steps in creating our model are described below:
rails g model Post content:string image:string like_id:integer comment_id:integer
This command will create a migration file for the post with the specified columns.
rails g model Comment content:string
This command creates a migration file for the comment with the specified column => content, rails will add a default id for every model, in this case, rails will generate a comment_id
rails g model Like content:string
Now, it is time we add our associations to our model.
class Like < ApplicationRecord
has_many :comments<br>has_many :likes
class Comment < ApplicationRecord
class Like < ApplicationRecord
So now, we have all our model and polymorphic association setup. The next step is to migrate our migration files with the command below
This command will create a schema.rb file in our db/schema.rb where we have all our 3 tables setup
Now, we can test our association with the help of the rails console. Type
To open the rails console. Now we can create post with the following command
p1 = Post.create(content: 'my first post', image: 'free.png')
This command will create and save a post in the variable p1. When we type p1, we should have the details of the post
<Post id:1, content: 'my first post', image: 'free.png'>
Now let us create our comment by assigning our first comment to a variable
c1 = Comment.create(content: 'the post is awesome')
This command will throw an error because the comment is associated with the post, so in order to fix this. Let check the command below
c1 = Comment.create(content: 'the post is awesome', post_id: 1)
With the post_id as the argument, the comment is now created successfully.
Now let us create 1 like as we did with the comment.
l1 = Like.new(content: 'i like the post', post_id: 1)
This command will create the like without saving it, so we need to save it with the command
This is why I like using the ‘create’ method in this article. Now we can play around the models and associations in the console. For more information and details explanation, let us visit the rails documentation https://guides.rubyonrails.org/active_record_basics.html