E-Commerce Web App in Ruby on Rails: 3 Cool Things I learned

Red Ebron
3 min readMay 5, 2021

There were so many things I have learned in the past few months and so much things I still don’t know about but right now let me tell you a few of the coolest things that helped me create a basic, fun and functional e-commerce web application in Ruby on Rails.

  1. Draw.io

I have always drafted my association tables before starting any project but this time I have added the Draw.io VS Code extension to my project folder — and it did make life easier to be able to quickly reference your tables while building the project! In the beginning, it was particularly difficult to visualize how the finish product will be. Drafting the models and association tables helped me understand how I want my app to actually work.

My e-commerce web app has 6 models.

A user who has many orders and oder items through orders.

An order that belongs to a user, has many order items, and has many products through order_items.

An order_item that belongs to an order, belongs to product, and belongs to cart.

A product that belongs to a category, has many order_items, has many orders through order_items, and has many carts through order_items.

A category that has many products.

A cart that has many order_items, has many products through order_items, has many orders through order_items.

Basic functionality includes a user being able to view all categories and products, add/remove products to/from the cart, and add/reduce product’s quantity in the cart.

I contemplated creating another model and migration for admin and then my Flatiron instructor brought up the basic enum usage to define a user role which is another cool stuff I will be talking about next.

2. Basic Enum Usage

For a small, beginner project like mine, it has saved me a lot of work (and brain cells from overthinking) when I can quickly and easily assign roles to any user.

First, I added the enum column to my existing User table.

class AddRoleToUsers < ActiveRecord::Migration[6.1]    def change        add_column :users, :role, :integer    endend

Next is to decide the initial list of roles I need for the User model. I only want two roles at the moment.

class User < ApplicationRecord    ...    enum role: %i(customer admin)end

As we don’t want every new user to be made admin, let’s setup our migration to have a user role’s default value of a :customer which is at index 0 of the enum role array.

class AddRoleToUsers < ActiveRecord::Migration[6.1]    def change        add_column :users, :role, :integer, default: 0    endend

Run the migration and get into rails console. Try the below commands and see for yourself what they are returning. Isn’t that cool?

user = User.new(name: "RedRidingHood")
user.role
user.customer?
user.admin?
user.admin!

3. Faker Gem

One of the tedious tasks a programmer has to do is seeding the database. Once my tables and migrations were in order, I proceeded to seed my database with the super cool faker gem.

Step 1: Add to Gemfile

gem 'faker'

Step 2: Install

bundle install

Step 3: Plant the seeds. Don’t forget to prefix your method call with .unique to ensure no data repetition.

7.times do     c = Category.create(         name: Faker::Commerce.unique.department,         description: Faker::Lorem.words     )      puts "Creating #{c.name}"end40.times do     p = Product.create(         name: Faker::Commerce.unique.product_name,         availability: true,         price: Faker::Commerce.price,         description: Faker::Hipster.sentence(word_count:            rand(4..8)),         category: Category.all.sample      )      puts "Creating #{p.name}"end

Cool, right?

Have fun coding!

--

--