Understanding Ruby Object Relation for absolute beginners.
Ruby is an amazing language that at its core is all about developer productivity. If you have had some basic understanding of Ruby you will be familiar with the concept of “everything in Ruby is an object”. If you are completely new to Ruby I will recommend you check out Learning Ruby: From Zero to Hero
Before we move forward we need to establish what objects mean in Ruby and then we can progress to how we can create a relationship between them. We have objects all around us, cars, house, people, trees etc we can say that everything around us is an object. We can both agree that each object as something that makes them unique and each unique object can also possess some attributes that make them different from their kind. The ability to model our Ruby program as an object gives us enormous power as a developer.
In this tutorial, we are going to be modelling a Restaurant, Customer and Review Objects. We are going to be establishing a relationship with these three unique Objects on how they relate with each other in real-world using Ruby.
Restaurant Object
We will start by describing our Restaurant Object, what type of attributes will a restaurant has, if we are to list there are so many for now we will only make it simple for the sake of our tutorial. The most important attribute a restaurant should have is a name. So let create a Class in Ruby that define our restaurant.
Above I describe a Class which we can refer to as a blueprint of what our restaurant object will be every time a restaurant is created. We want that restaurant to have a name. So creating a new restaurant will look like this
kfc = Restaurant.new("KFC")
` before we move on we are going to add simple functionality to our Restaurant class, wee need our Restaurant to be able to keep track of each instance of Restaurant created throughout is a lifetime. This will be like a simple internal database for us.
Customer Object
We will also describe a simple Customer by setting it’s most important attribute a customer name.
The Customer class is almost equal to the Restaurant class we described above the only obvious difference is that we wanted a customer to be instantiated by our user entering their full name then we can set the first name and last name for them from their input, clever right.
Review Object
Before we dive into the review object let us establish a basic concept of relationship. A Customer can has-many Restaurant through Review and a Restaurant also has-many Customer through Review. This is called has many through relationship.
The Review Object can be conceptualized as a joiner between the Customer and Restaurant and a Review will not exist without any of both.
In the above code, we describe a class for our Review of the object taking in four arguments on initialization, the review content, rating and an instance of a restaurant and customer respectively. You should note here the value of customer and restaurant we are passing in is an instance and not a string like content or integer like rating, we see this clearly during the instantiation. The essence of passing in both the restaurant and customer instance is to build the relationship so every review knows the restaurant and customer they belong to.
Now that we have established the relationship between the three models we can now start building aggregation methods that can give us information about the relationship.
Associations and Aggregate Methods
We will start with the customer what are the operations a customer can carry out.
Customer can create a review
Customer#add_review(restaurant, content, rating)
given a restaurant object, some review content (as a string), and a star rating (as an integer), creates a new review and associates it with that customer and restaurant.
Above is an instance method that allows a customer to create a review, you will see that will pass in the customer instance as self, the self keyword here refers to the customer creating the review.
The number of Reviews created by a customer
Customer#num_reviews
Returns the total number of reviews that a customer has authored
All the Restaurant the Customer as reviewed
Customer#restaurants
Returns a unique array of all restaurants a customer has reviewed
Methods for the Restaurant
Restaurant#customers
Returns a unique list of all customers who have reviewed a particular restaurant.
Restaurant#average_star_rating
returns the average star rating for a restaurant based on its reviews
Restaurant#longest_review
returns the longest review content for a given restaurant
You see how the Object relation gave us an intuitive way of modelling methods that each of our objects can take advantage of to get information about themselves and other objects they are related.
As we built instance methods that we can call on each instance of Customer or Restaurant we can also define some Class methods that can be call on each class itself, let’s do that now.
Customer
class
Customer.find_by_name(name)
given a string of a full name, returns the first customer whose full name matches
Customer.find_all_by_first_name(name)
given a string of a first name, returns an array containing all customers with that first name
Customer.all_names
should return an array of all of the customer full names
Restaurant
class
Restaurant.find_by_name(name)
given a string of restaurant name, returns the first restaurant that matches
Wow it a long ride here!
Find all the code so far here and some test data
Thanks for reading, if you have any question drop it in the comment, working on a beginner introduction to Ruby for those who are new to Ruby. Follow me to stay in the loop.