Building a Recommendation Algorithm in Ruby on Rails (Part 1)

The Types of Recommendation Systems

There are 2 widely used systems to recommend products to customers, whether that product be physical goods like Amazon sells, television shows or movies like Netflix, or even other user’s profile pages like Facebook.

Collaborative Filtering

Collaborative filtering compares the likes, dislikes, and trends of user’s to match them with other similar users, and then recommend them products based on what the other similar user’s enjoy. For example, if User A watches a bunch of the same Rom-Coms on Netflix as User B, and both users rate them similarly, if User A finds a new show they like, it will then be recommended to User B. This approach has advantages as it allows the system to recommend things to the user that may be completely outside of their normal likes or behavior. The disadvantage, however, is that it requires many users and many ratings for each user in order to build an effective system.

Content-Based Filtering

Content-Based filtering uses the traits of one product, to associate it with other similar products in order to make recommendations to users. This is the much simpler system to use for sites or applications without many users at the beginning as it lets you make recommendations without needing a ton of data on other user’s behavior. This is the system we will be using for the purpose of this article today.

Building the Algorithm

In order to build this algorithm we will begin by setting up a Ruby on Rails Application, and seeding it with some products we’d like to make recommendations for. For our example today, I will be using board games as the product, with data pulled from the Board Game Atlas API.

Board Game Atlas provides tons of information on each game, which we will use to create attributes for each.
https://api.boardgameatlas.com/api/search?order_by=popularity&ascending=true&client_id=[YOUR_CLIENT_ID_HERE]

Storing the data from the API in your Ruby backend

For simplicity, we’re going to use a Ruby gem called Rest Client to perform the request to the API endpoint detailed in the last section.

require ‘rest-client’
games_array = RestClient.get('https://api.boardgameatlas.com/api/search?client_id=Af1TZudirh')

Iterating through games_arry to create instances of the Game class

Now before this next step, you’re going to want to make sure you have a model and controller set up in your Rails application called ‘Games’. The quickest way to do that is by using ‘rails g resource Game’ to set up all of the necessary components. For more info on this step, read this blog post.

games_array.each do |game|Game.create(name: game[:name],image: game[:image],price: game[:price],max_players: game[:max_players],min_players: game[:min_players],board_score: rand(10),players_score: rand(10),card_score: rand(10),party_score: rand(10),)end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store