Ruby Bindings for E-Commerce Tutorial
One of the main things that people often want to do is to integrate Directed Edge's recommendation engine into their online store. Many of those stores are using Ruby already, so we decided to give a very concrete walk-through of doing recommendations based on previous customer purchases starting from exporting data all the way to keeping it in sync.
If you want to see all of the code in one go, you can get it in full syntax-highlighted glory here: ExampleStore Class, or download it here. You'll also want our Ruby bindings, which you can get from GitHub here (download).
Creating the testing database
You've probably got your own site's database that you're more interested in, but if you want to follow along in the code, you can use the store data that we randomly generated. We created 2000 users with 500 products and had each of those users "buy" between 0 and 30 products.
For that we created three very simple tables. Since we don't care about the other properties of the customers or products, all that those tables contain is an ID. The purchase table just contains customer and product columns.
customers table:
|
products table:
|
purchases table:
|
It really can't get much simpler than that. You can get a dump of the database here. If you have a local MySQL running, you can create and import the database with these commands:
$ mysql --user=root
You should now be at the MySQL prompt:
mysql> create database examplestore; mysql> create user 'examplestore'@'localhost' identified by 'password'; mysql> grant all on examplestore.* to 'examplestore'@'localhost';
Now back at the command line do:
$ mysql --user=examplestore -p examplestore < examplestore.mysql
Unless you changed the password above, the password is just password. You've now got the same data that the examples use imported to a database called examplestore.
Setting up the Ruby plumbing
Here we just do the standard ruby hashbang and import three modules. Ruby gems is required since we've pulled in activerecord via gem. ActiveRecord is the object relational mapping that is standard with Ruby on Rails and we'll be using it to access our MySQL database. And finally, we use the [Ruby bindings for the Directed Edge API.
#!/usr/bin/ruby
require 'rubygems'
require 'activerecord'
require 'directed_edge'