Gaining a Better Understanding of Rails and Object Relationships
For this project I really wanted to push myself and create something different than just a standard form app. After a lot of brainstorming I decided to create a trivia quiz game web app. The intention of this is app is to be more of a social platform rather than just a pure trivia app. Instead of following the traditional format of questions and answers created by a developer or admin this app’s quizzes are purely user generated.
A user can create their own quiz with 5 questions and answers a name and a cat gory of categories. A user can also go and take quizzes generating a score for every game played. Each quiz show page has a leaderboard showing the top 5 scores and usernames of the users that have taken the quiz. The top 5 scores for each quiz are generated by a Game class method that joins the two tables together to show what users scored the top 5 spots for each quiz.
The game mechanism works in the Game controller. By using a new method in the game controller guesses are submitted. In the game model there is a method that takes the guesses submitted in the game form and compares them the answers from the question and answer class. For every correct answer that is submit a point is given to the score column in the games table. Each game has a score, user_id and quiz_id column. These columns are how the app keeps track of what quiz and user the game belongs to. With these relationships the app is able to show scores and give the list of who has played the quiz as listed on the quiz show page.
The game is played through nested routes. When the score of a game is calculated a RESTful route of /quizzes/quiz_id/game_id is the show page. The game play itself is also achieved with nested routes and uses the route /quizzes/quiz_id/new to submit the quiz. This quiz is Restful but also helps keep track of the quiz id which is needed for the game play.
This app uses serval different nested forms. Nested forms allow various objects to be updated at the same time. This is used when questions and answers are given to build the quiz as well as when guesses are given. While editing and creating nested forms the controller and model must have the proper methods/permissions to updated using the modelname_attributes. In the model this can be achieved by using a method that rails gives you, accepts_nested_atributes_for. This app uses a custom setter for modelname_attributes= to validate that good data is being submitted to the database.
Rails is an incredible framework with many built in methods and helpers. In order to keep the app dry a developer can also create custom helpers. This app uses helpers to validate and help the app only have methods listed in one location.
Overall this rails app was an incredible learning experience. Rails by convention makes it easy to stay on the right path to keep routes restful. Also, the built in helpers make typing href links incredibly simple. However, the biggest challenge and yet greatest feature of rails is the relationships given to objects with has many and has many through. Complex relationships is the only way this app can work and rails makes that connection very simple with its macro methods of has_may and belongs to. This was a great learning experience and I look forward to incorporating javascript with rails in my next project.