Other Pages

Expand All

Voting On Topics


  • Create a model for votes

  • Votes

    Every topic in suggestotron can be voted on. In order to count votes, we need to record votes. We'll add that table now.


Type this in the terminal:
rails generate model vote topic_id:integer
rake db:migrate


  • Just like before, we're creating a new model named "vote"
  • The only thing really different is the integer we added called topic_id.
    • topic_id is the data we need to draw the line between votes and topics.
  • We didn't generate a full scaffold this time because we aren't going to do the full CRUD for votes; they're just going to be considered part of topics as-is.

After running the rails generate resource command we are sure you noticed that it does something really similar to the rails generate scaffold command. The difference is:

  • The command rails generate scaffold <scaffold-name> generates prepopulated controllers, views, models and migrations. Also, it hooks up everything together so you can easily do CRUD (Create, Read, Update, Delete) operations without writing any code.
  • The command rails generate resource <resource-name> generates empty controllers, models and migrations (no views!) so you, the programmer, can later write your own code in those files and customize the behaviour of the application for your needs.

Now that you know what is the difference between scaffold and resource, lets continue with the next step.

Next Step:

If you find something that could be improved, please make a pull request or drop us a note via GitHub Issues (no technical knowledge required).

Source: https://github.com/RailsBridge-CapeTown