Sign in

Many-to-Many Relationship and Simple Calendar Rails Gem

I decided to create a Salon Managing application for my third project at Flatiron Bootcamp.

The requirements included creating a many-to-many relationship while using the Ruby-on-Rails framework. The strategy I’ve become familiar with so far is implementing the has_many :through method within our class models.

Creating a join table allows us to have a many-to-many relationship between our tables.

In this case, a client sees many stylists, through their appointments; while stylists have many clients, through their appointments.

We have to create associations between our models for this join table to take effect.

Ruby on rails guided provided a wonderful example:

class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments

class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient

class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments

I wanted to be able to view our appointments on a calender view; however the thought of creating a calendar from scratch in so little time seemed daunting, and the simple_calender gem came to the rescue.

The creator provides easy instructions on installion here:

Here’s is my customization of the method provided to display events. This method already included our start_date params connection to todays date. Instead of events, we are trying to display our appointments, and instead of all of them, we only want appointments associated with the user signed in.

def index
if same_stylist
start_date = params.fetch(:start_date,
@appointments = @stylist.appointments.where(day: start_date.beginning_of_month.beginning_of_week..start_date.end_of_month.end_of_week) elsif same_client
start_date = params.fetch(:start_date, = @client.appointments.where(day: start_date.beginning_of_month.beginning_of_week..start_date.end_of_month.end_of_week)
start_date = params.fetch(:start_date,
@appointments = Appointment.where(day: start_date.beginning_of_month.beginning_of_week..start_date.end_of_month.end_of_week, user_id:

The end result of what the gem provided was impressive and the gem is easy to use.