It’s Rail Good! Food For Fido

Photo by Adam Thomas on Unsplash

I think you meant real?

Do you remember our good old friend Sinatra ? Well, Rails is basically Sinatra but better! It is a web application framework that has everything you need to create an application utilizing a Model-View-Controller design. I say that it’s Sinatra but better because unlike Sinatra, Rails has a lot more “magic” and requires less repetition when dealing with routes and controller actions. This allows you to DRY up a lot of your code (because we programmers are lazy and like efficiency).

So let’s get into my rails project!

Minimum Viable Project Requirements

  1. Model must include the following associations: A has_many, A belongs_to , Two has_many :through. This will be your many-to-many relationship.
  2. The join table must have user-submit table attribute
  3. Must include appropriate validations to protect against bad data
  4. Must include at least one ActiveRecord scope method. This scope method must be chain-able with an ActiveRecord Query method.
  5. Must have standard user authentication
  6. Authentication system must allow the option to login from a different provider (e.g. Facebook, Google, Github)
  7. Must include nested routes for new and index
  8. Forms should appropriately display validation errors
  9. Application must be relatively DRY
  10. DO NOT use scaffold to build project

The Approach

The beginning of the second week rolls around and I touch base with my amazing cohort lead. She takes a look at my schema and tells me that I can actually condense my models so that my associations can be a lot more simple. This gave me so much hope for the future of this project because at that point, I really wasn’t sure if I was going to make it.

So after MUCH deliberation, I finally came up with the project ‘Food For Fido’. This web application allows fellow dog owners or enthusiasts to find fresh food recipes for their pup! The user can create an account to rate and comment on recipes. So let’s break down this project.

Starting Up Rails

rails new ‘food-for-fido’

cd ‘food-for-fido’

Once in the project, I ran bundle install.

I will eventually run bundle install a couple more times after adding a couple more gems but we’ll get to that later.

Models and Associations

User associations in app/models/user.rb
Comment associations in app/models/comment.rb
Recipe associations in app/models/recipe.rb

My schema:

After this, everything else seemed like it came together pretty quickly thanks to our lovely generators!

Generators

rails g resource User name:string email:string password_digest:string

This created the controller, view, helper, migration files, and routes but did not create any code for the files other than the routes. The resource generator basically gives you an outline of the things that you need to build out a project and you write the code you need inside those files. It is more customizable and less prone to unnecessary files and errors. One of the very amazing things that Rails has to offer! I was actually so surprised how fast this built the outline of my project. All I needed to do now was to create the most important part, the code!

Validations

validations for recipe in app/models/recipe.rb

Wait..Omniauth?

CRUD and Routes

routes in config/routes.rb

Since we are dealing with associations, especially a many-to-many relationship, we also needed to create nested routes. Since we have two parents and a child, nested route allow us to display this relationship in the routes as such:

nested route as recipe has_many comments

By creating a nested route, this also created more path helpers to use to link_to our show page for the child.

Dealing with these many to many relationships you can only imagine the amount of code I needed to create for this project, not to mention repeating code. To DRY up my controllers and views, I created helpers and partials.

Partials/Helpers

Debugging

Final Thoughts