Difference between revisions of "Ruby Bindings for E-Commerce Tutorial"

From the Directed Edge Developer Base
Jump to: navigation, search
(Setting up the Ruby plumbing)
Line 71: Line 71:
 
require 'directed_edge'
 
require 'directed_edge'
 
</source>
 
</source>
 +
 +
== Setting up ActiveRecord ==
 +
 +
ActiveRecord handles connecting to the database for us and is the mechanism that most Ruby web apps use to do the same.  To connect with ActiveRecord we have to supply the usual information &mdash; database type, host, user name, password and database name.
 +
 +
Based on the database that we imported above, these connection values should work.
 +
 +
ActiveRecord also handles most of the magic of mapping Ruby classes to database tables.  It can figure out that the ''customers'' table corresponds to the ''Customer'' (and the some for ''products'' and ''purchases'') just by inheriting from the ActiveRecord base class.
 +
 +
<source lang="ruby">
 +
ActiveRecord::Base.establish_connection(:adapter => 'mysql',
 +
                                        :host => 'localhost',
 +
                                        :username => 'examplestore',
 +
                                        :password => 'password',
 +
                                        :database => 'examplestore')
 +
 +
class Customer < ActiveRecord::Base
 +
end
 +
 +
class Product < ActiveRecord::Base
 +
end
 +
 +
class Purchase < ActiveRecord::Base
 +
end
 +
</source>
 +
 +
In place of ''Customer'', ''Product'' and ''Purchase'' you'll want to substitute in the values that correspond to your database.

Revision as of 15:02, 25 May 2009

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:

id

products table:

id

purchases table:

customer product

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'

Setting up ActiveRecord

ActiveRecord handles connecting to the database for us and is the mechanism that most Ruby web apps use to do the same. To connect with ActiveRecord we have to supply the usual information — database type, host, user name, password and database name.

Based on the database that we imported above, these connection values should work.

ActiveRecord also handles most of the magic of mapping Ruby classes to database tables. It can figure out that the customers table corresponds to the Customer (and the some for products and purchases) just by inheriting from the ActiveRecord base class.

ActiveRecord::Base.establish_connection(:adapter => 'mysql',
                                        :host => 'localhost',
                                        :username => 'examplestore',
                                        :password => 'password',
                                        :database => 'examplestore')

class Customer < ActiveRecord::Base
end

class Product < ActiveRecord::Base
end

class Purchase < ActiveRecord::Base
end

In place of Customer, Product and Purchase you'll want to substitute in the values that correspond to your database.