Friday, September 23, 2011

Polymorphic Association in Rails , Single Table Inheritance – Part I

From wikipidiea:
 “Polymorphic association is a term used in discussions of Object-Relational Mapping with respect to the problem of representing in the relational database domain, a relationship from one class to multiple classes. In statically typed languages such as Java these multiple classes are subclasses of the same superclass. In languages with Duck Typing, such as Ruby, this is not necessarily the case”.
In Rails, ActiveRecord provides you feature by which you can have Polymorphic association (relationship) between two tables without knowing which table will be in the relationship. You can have “XOR” relationships. That is, X can have either of Y or Z, but not both.
There are basically two ways of achieving Polymorphic Association in Rails:
1.       Single Table Inheritance  (STI)
2.       Multiple Table Inheritance (MTI)
Single Table Inheritance
In STI, you can have only one single SQL table to maintain the relationships between two or more different model class of rails.
Let us assume for the explanation purpose, that we want to build a mobile catalogue system.  Mobiles are of various types based on their OS, lets categorize our catalogues system based on the OS of mobile.
So our model definitions will be something similar to this:
Mobile Class:
class  Mobile < ActiveRecord::Base

end
IOS class:
class IOS < Mobile
end
Android class:
class Android < Mobile
end

In the mobile table, we will have to put a column called "type" to indicate that the table is STI.
The table structure will be something, similar to this:
CREATE TABLE  mobiles (
   id INT NOT NULL AUTO_INCREMENT,
   name VARCHAR(25) NOT NULL UNIQUE,
   price VARCHAR(10) NOT NULL,
   type VARCHAR(10) NOT NULL,
   PRIMARY KEY (id)
);


The “type” column should store the class names, i.e the class type for each mobile. So in this case, it will be either IOS or Android.

In the next part, we see how we can achieve, Multiple Table Inheritance in rails.

Thursday, September 15, 2011

Complete guide for setting up Starling

Complete guide for setting up starling. It includes installation, configuration and monitoring of the services 

Dynamically generating methods with the help of define_method in Ruby

Ruby language provides a very interesting feature with the help of which you can dynamically generate methods at run-time. Yes you have read it correctly, you can write codes which will generate methods for you :).


So how can you do it in ruby ? Well, Ruby provides a method called define_method. This method is a private class method


Here is the syntax :
define_method(symbol, method)
define_method(symbol) { block }

where,
  • symbol is the method name; it can be either a symbol, a string, or a variable whose value contains the method name.
  • method is a lambda or Proc object containing the method logic. 
Let me define a very simple method, which will print a hello message for the demonstration purpose. This example will generate an instance method, therefore we will have to create a object for printing the hello message


class D
  define_method(:hello) do |h|
    puts "Hello !!"
  end
end
C.new.hello('Jack')   => Hello Jack !!



Note that, this is a very simple example. Here I wanted to describe the basic idea of define_method provided in Ruby.

Managing multiple version of Ruby and Rails in Windows: PIK

Here I will give a breif description about a tool called Pik (https://github.com/vertiginous/pik), a ruby version manager for Windows.


With the help of Pik, one can have multiple version of Ruby and Rails in his system. Find below the steps that are required for installing Pik:


1. Go to RubyForge and download Ruby. If Ruby is already present, then skip step 1.
2. For installing pik, run the following command from your terminal. gem install pik. You should see ‘Successfully installed pik-0.2.8‘ message soon enough
3. After downloading this gem, you will need to intall pik in some directory other than "C:\Ruby\bin". You can perform the following steps:
    a. d:
    b. mkdir pik
    c. Include the newly created directory, i.e D:\pik in the PATH environment variable.
    d. pik_install D:\pik
Pik will be now installed in your system.



Using Pik


I can now start using Pik for mantaining multiple versions of Ruby and Rails !!.


>pik
** Adding:  187: ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]
Located at:  C:\Ruby187\bin
Usage: pik command [options]

Executing pik list outputs all the ruby installations it knows about. 



>pik list
* 187: ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]
  192: ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
 
As Rails 3.0 requires 1.8.7 or higher, let’s install the latest version of Ruby.



You can then switch to the particular version you want, for example originally I was running 1.8.7.


>ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]

But I can then switch to 1.9.2 with a simple command.



>pik switch 192

>ruby -v
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]


Now by switching the versions of ruby we can install and have different rails version. For example with ruby version 1.8.7, I can have rails 2.3.8 version and I can have rails 3.0 version by switching the ruby version to 1.9.2 !!