In our app postit, the vote
function is applied to both Post
model and Comment
model. A post has many votes and a comment also has many votes. In this case, we will use polymorphic associations.
First we will add votes table, rails generate migration create_votes
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
In the command line, run rake db:migrate
.
:voteable_type
column will record the model class name and :voteable_id
will record the object ID that’s voted on.
Then the following changes should be applied,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Now we can retrieve @post.votes
and set the vote by vote.post = Post.first
. Good to go!