Thursday, December 22, 2011

Take Screenshots in Linux/Fedora

Many a times we need to take screenshot. Today, I have installed kdegraphics for having the screenshot feature in my Fedora v14 OS.
The installation procedure is pretty simple :)


You simply have to do sudo yum install kdegraphics and it will install the application for you along with the required dependencies. 

Now you can take screenshots. You can launch the application from Applications->Accessories->Take Screenshot

You will get 3 different options for taking the screenshot.

Wednesday, December 21, 2011

POSTGRESQL: Ident authentication error

Today,

I was trying to configure POSTGRESQL server in Fedora Linux OS system. On my surprise, when I typed:

# psql -d railsapp_development -U rails_db_user -W

It gave me the following error:

# psql: FATAL: Ident authentication failed for user "username" 

For, solving this issue, you will have to edit the following file:

vim /var/lib/pgsql/data/pg_hba.conf

This file has various functionalites apart from controlling how the user/clients are  authenticated. By default, POSTGRESQL, supporst IDENT based authentication. IDENT based authentication will not allow you to login with -U and -W option. 

The following changes has to be done inside pg_hba.conf file:

# Type    Database     User     CIDR-ADDRESS            METHOD
----------------------------------------------------------------------------------------------------
  local        all               all                                                   trust
  host         all               all          127.0.0.1/32                     trust 


Basically you will have to replace ident with trust

Now restart the server with the following command:

# /etc/init.d/postgresql restart

After this you will be able to login. 

Note, that I was facing the above mentioned error, when I was trying to invoke the command rake db:migrate in rails.


But after doing the above mentioned changes, migrations started working fine.

Thursday, November 10, 2011

Rails, SQL Server 2008 & Windows - Tips and Tricks !!

In this post, I will be discussing in short about the configuration one need's to do for Rails, SQL Server 2008 and Windows XP system.


My configuration in my current system is:


1. Ruby: ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-mingw32]
2. Rails 2.3.8 
3. OS: Windows XP.
4. SQL Server 2008


Note, that I have downloaded Ruby from rubyinstaller site, the link is mentioned below in this post.
First of all, we need two gems to be installed in our system to work with SQL Server 2008


1. activerecord-sqlserver-adapter, command for installation: gem install activerecord-sqlserver-adapter -v=2.3.6


2. ruby-odbc, command for installation: gem install ruby-odbc


Please note, without these two gems thing will not work properly.


activerecord-sqlserver-adapter has other higher versions, which i presume works with rails 3.0 + versions. For my purpose, I have used the version, 2.3.6
Before installing, ruby-odbc gem, you will need to install the ruby development kit from the ruby installer site. The links for rubyinstaller site are:


1. http://rubyinstaller.org/
2. http://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe
3. https://github.com/oneclick/rubyinstaller/wiki/Development-Kit (installation instructions can be found out here)


Without installing the devkit, the ruby-odbc gem will not be able to install.


Now, in the next step, we will have to create  dsn link for odbc connection.You can get a idea about to create it by going through this link: http://www.ehow.com/how_4642566_setup-odbc-database-connection.html


Once, the dsn link is done and lets assume that you have named it as dsn_link, you have to enter the information in your database.yml file. Here is a snapshot of my database.yml


development:
  adapter: sqlserver
  mode: odbc
  dsn: dsn_link
  username: my_username
  password: my_password



This is it !!. Restart your development server and now your application will be pointing to the SQL server configuration :)

Friday, October 28, 2011

Gem.source_index is deprecated, use Specification ( ERROR in Rails)

Hi,


Today, while setting up my new system with Rails, I was getting the following error when starting mongrel/webrick dev server:




NOTE: Gem.source_index is deprecated, use Specification. It will be removed on or after 2011-11-01.
Gem.source_index called from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.8/lib/rails/gem_dependency.rb:21.
NOTE: Gem::SourceIndex#refresh! is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#refresh! called from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.8/lib/rails/vendor_gem_source_index.rb:34.
NOTE: Gem::SourceIndex#load_gems_in is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#load_gems_in called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:322.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.


My configuration for rails was:


Ruby version 1.9.2
Rails version 2.3.8
gems version 1.8.11


OS version: Windows XP.


I solved the problem very easily. If you notice in the configuration section, my gem version is 1.8.11. This is causing the issue. So, I downgraded my gem version to 1.6.2 and the problem was SOLVED !!.


The following is the command for downgrading the gem version:


gem update --system 1.6.2


Hope that this small instruction will help someone !!







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 !!