Og is a tool for Object Relational Mapping (ORM). You can read more about ORM at http://en.wikipedia.org/wiki/Object-relational_mapping. At it’s most basic, it’s a way of storing the data from any instantiated objects in a relational database. Og allows you to map your class to a database, without having to know or care about what the tables look like underneath.
Assuming you have ruby and rubygems installed, installation of Og is straightforward. Install the gems as a super user like so:
> sudo gem install og --include-dependencies Attempting local installation of 'og' Local gem file not found: og*.gem Attempting remote installation of 'og' Successfully installed og-0.28.0 Successfully installed gmµçVRÓ ã#‚ã УÂö6öFS</p> <p>See http://www.ruby-lang.org, and http://www.rubygems.org for information on how to install of these if you haven’t already.</p> <p>If you want to install Nitro and Og from source, you will need to visit http://rubyforge.org/projects/nitro and download og and glue (and probably the examples). You’ll also need facets from http://rubyforge.org/projects/facets. Take each file, untar it, change to the directory created and run ‘ruby setup.rb’ as superuser. I’d recommend sticking with the gems though – it makes life much easier, and has become a de facto standard for installation in the ruby world.</p> <h3>Setting up the database</h3> <p>Now that everything is installed, you’ll need to set up a database to represent any data your application is going to create. In this tutorial, I’m going to create an application for reporting bugs/enhancement requests in a software product and then following their progress through the development life cycle. This application is called ‘Follower’. I’m going to use the PostgreSQL database throughout this tutorial, but you can use your choice from SQLite, MySQL, or PostgreSQL. Although KirbyBase is listed as a supported database, in reality it is less used by the Og community than the other three, and therefore receives less testing and fixing. For a production application, I’d recommend PostgreSQL, as this is the most heavily tested.</p> <p>I like to create the user with the same name as the application, so that I know which users should be accessing which tables. I’m no database expert, so I’ll leave it to the reader (and the reader’s db documentation) to figure out how to create a user. Make sure the user has privileges to create databases, as this will come in handy later. Then create a database with an appropriate name (I’ve used ‘follower’ for user name, password and database name).</p> <h3>Installing PostgreSQL</h3> <p>Visit http://www.postgresql.org to download the appropriate version for your operating system. Full installation instructions can be found on that site.</p> <p>You’ll then need to install the ‘postgres’ gem locally. This is a wrapper around the PostgreSQL <span class="caps">C API</span>, which allows ruby to talk to your database. Install this with:</p> <p><code syntax="shelll"> > sudo gem install postgres
or visit http://rubyforge.org/projects/ruby-postgres/ for alternatives (and troubleshooting info).
Once you’ve installed everything, you’ll need to set up the database and user. To pull from my example above:
> psql -U postgres postgres=# create user follower password 'follower' createdb; CREATE ROLE postgres=# </co²–§rV¬±Æ©±‘”þÊZ</p> <p>The ‘create user …’ command will create a user with a password and a database with the same name, which is owned by that user. If you want to name your database differently:</p> <p><code syntax="shell"> postgres=# create database some_other_name with owner = some_user_name;
You’re now ready to Og on PostgreSQL.
Visit http://www.mysql.com to download the appropriate version for your operating system. Full installation instructions can be found on that site.
You’ll then need to install the ‘mysql’ gem locally. This is a wrapper around the MySQL C API, which allows ruby to talk to your database. Install this with:
> sudo gem install mysql
or visit http://www.kitebird.com/articles/ruby-mysql.html for alternative instructions for downloading and installing from source.
Once you’ve installed everything, you’ll need to set up the database and user. To pull from my example above:
> mysql -u root mysql> create database follower; Query OK, 1 row affected (0.29 sec) mysql> grant all privileges on follower.* to follower@localhost identified by 'follower'; Query OK, 0 rows affected (0.53 sec) mysql> flush privileges; Query OK, 0 rows affected (0.35 sec)
These three instructions will create the correct database, user and password, and give all the necessary permissions to the ‘follower’ user to use the database. ‘flush privileges’ ensures that the commands you’ve given regarding users and permissions get applied to your existing session – it’s not really needed in this case, but I include it for completeness sake.
Visit http://www.sqlite.org to download the appropriate version for your operating system (this step is usually not needed for Mac OS X users). Full installation instructions can be found on that site. SQLite is not a database engine per se, but rather a library which allows creation of SQL databases which are stored in flat files on your file system.
You’ll then need to install the ‘sqlite3-ruby’ gem locally. This is a wrapper around the SQLite C API, which allows ruby to talk to your database. Install this with:
> sudo gem install sqlite3-ruby
or visit http://rubyforge.org/projects/sqlite-ruby/ for more information.
The cool thing about SQLite is that you have no need to set-up anything more than those two things. The name that you specify for the database in your Og.setup (see below) will actually be a file name with a SQLite database in it.
Now I have all the tools I need to be able to create my ‘Follower’ application. In any application, it is generally accepted that you’ll be dividing your logic into three parts: the model, the view and the controller. Read all about that at http://en.wikipedia.org/wiki/Model_view_controller. At it’s most basic, the model is the way you represent your data, the view is how you display your data, and allow interaction with it, and the controller is the bit that, um, controls the interaction between the view and the model – where your application’s logic lives.
So, Nitro is for creating the view and the controller, and Og plus a database is for creating and storing the model. This tutorial only looks at Og – I haven’t learned enough of Nitro to be able to write a tutorial on it yet!