Thursday, June 14, 2018


Eid-al-Fitr in Dakshina Kannada district on Friday

| TNN | Jun 14, 2018, 20:26 IST

32
Representative image. Representative image.

MANGALURU: Muslims in Dakshina Kannada district will celebrate Eid al-Fitr on Friday.

Dakshina Kannada district khazi Alhaj Twaqa Ahmed Musliyar has made an announcement in this regard on Thursday.


He said the decision to celebrate Eid-al-Fitr on Friday was taken based on the first moon sighting of Shawwal witnessed at Kozhikode on Thursday. Special prayers will be held at all masjids in the district at 8am.

Wednesday, June 19, 2013

Prepending in a Ruby Array

Appending in Ruby array is done in the following way :

my_array = ['s','a','u','r','a'] => ["s", "a", "u", "r", "a"]
my_array << 'v' => ["s", "a", "u", "r", "a", "v"]

Now, if you want to prepend any value to an array, Ruby's unshift method can be used for this purpose.

my_array => ["s", "a", "u", "r", "a", "v"]
my_array.unshift('c') => ["c", "s", "a", "u", "r", "a", "v"]

Thats it !

Monday, June 10, 2013

Ruby's try()

I came to know about Ruby's try() method today, liked, the overall purpose of this method and so, sharing a brief note about how we can use this method while writing codes. 

Many a times, we have to write conditions for checking for nil values/objects. try() can help us to write clean codes whereby we don't have explicitly do testing for nil value. 

Have a look at the small example below 

Without try():
puts @student.school.name if @student.school
With try():

puts @student.school.try(:name)

Read this http://apidock.com/rails/Object/try link, to know about try() method more in details.


Tuesday, February 12, 2013

SQL logs in Rails console


While working on different Rails projects, I often find it useful to see behind the scenes SQL queries in the console itself. We can the view the SQL queries in the log file itself, but I find it easier to look at the SQL queries at the console itself.

For this purpose in my Ubuntu 11.10, I have created a file called .irbrc in my home directory and have pasted the below contents in that file.

1:  require 'pp'  
2:  if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')  
3:       require 'logger'  
4:        RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)  
5:  end  


Now after starting the console, the output for any ActiveRecord operation shows the sql query also

Here is a simple output from the console
1:  >> User.all(:limit => 1)  
2:   User Load (0.9ms)  SELECT * FROM "users" LIMIT 1  

Not, I could test this only in Rails 2.

MailCatcher gem: Testing emails in Development mode in Rails

Well, I am writing this post after a long long time. Had some personal priorities to take care of.

Few days, back I was given a task, to develop a module where I had to write code for sending notification mails to different kind of users based on different actions/events in the application. Sending mails with ActionMailer is not that tough, but in my development environment I was not available to find  out how will the contents of the mail will actually look like when it is sent to the users.  I needed some simple way to test it.

While searching in google, I came to know about the mailcatcher gem and I am glad that I found it very easily :)

It is very easy to setup and configure mailcatcher in your project. 

First, you will add the following line in your gemfile

 gem 'mailcatcher'  
bundle install
Then you have to do the following changes in config/development.rb
 config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 }
Then start/restart your rails app and mailcatcher daemon from terminal just by typing mailcatcher
saurav@saurav-Vostro-1540:~/workspace/my_project$ mailcatcher 
Starting MailCatcher 
==> smtp://127.0.0.1:1025
==> http://127.0.0.1:1080  
Now on opening http://127.0.0.1:1080 in your browser and you can see a virtual inbox and if you send some mails from your application, you will be able to see the mails in the inbox created by 'mailcatcher' gem. 

The most important feature of this gem, is that it allows you to see the HTML,Plain Text and Source view of the mails.

Thats it. Now test your mails :)

Friday, August 17, 2012

How to install MongoDB on Ubuntu 11.10

MongoDB can be installed in the following way

then inside /etc/apt/sources.list, append the following line Now add the GPG key and update your package list: Now we can install mongoid db in the following way Now you can check whether mongoDB got installed in a proper way: From your browser also, you can hit the url mentioned below and check whether mongoDB got installed properly.

Sunday, July 15, 2012

Generating Excel file with 'writeexcel' gem & 'axlsx' gem

Well, here I am describing a very short example of creating excel files in ruby. For this example, I have used the gem "writeexcel" writeexcel.

Here is a very small example

Well, Now a small example for generating excel file with axlsx gem.


Sunday, June 17, 2012

Could not find a JavaScript runtime (ExecJS::RuntimeUnavailable)

Hi,  

If you are using 'execjs' gem and if you get the below mentioned error, while starting the application server or executing rake tasks or even starting the console, then you might have to add the gem 'therubyracer'. 

Today, I was getting the following error in my system after installing 'execjs' gem:

/home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could ot find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/execjs-1.4.0/lib/execjs.rb:5:in `<module:ExecJS>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/execjs-1.4.0/lib/execjs.rb:4:in `<top (required)>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `<top (required)>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `<top (required)>'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
from /home/saurav/.rvm/gems/ruby-1.9.2-p318/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `<top (required)>'
.......
.......

The solution was to add the following line gem 'therubyracer' to the Gemfile and then execute 

bundle install

Voila !!....I don't see the error any more :) 




Tuesday, May 1, 2012

Referring to class from class method and instance method

Instead of directly writing ClassName for calling methods, this can be done in a different way by writing self.class

Below is a small example: 

Output will be:

Class Name is SomeClass

Class Name is SomeClass

Wednesday, April 25, 2012

Change Column or column type in Rails

Hi, 

If you have to change the column_type of an existing column in a table, you have to create a new migration and do the following changes:

saurav@saurav-Vostro-1540:~/workspace/project$./script/generate migration ChangeNotes

The above command will generate a new migration file and the migration should be like this after you have made the change:

class ChangeNotes < ActiveRecord::Migration
  def self.up
      change_table :notes  do |t|
        t.change :note, :text
       end
    end

    def self.down
      change_table :notes do |t|
        t.change :note, :string
      end
    end
end

where "note" is the column for which I have changed the data-type. Earlier, the data type of "note" column was set to "String" and now it has been set to "text"..

Thursday, February 16, 2012

RVM - No same user error fix

Hi All, 

Today, after installing RVM in my system i was getting a strange error "no same user error" whenever I was trying to install ruby with rvm. I was getting error of similar sort: 
"Error opening archive: Failed to open '--no-same-owner': No such file or directory"
It is actually a bug in rvm, see this commit: 
https://github.com/wayneeseguin/rvm/commit/52018750763d5321b7b993e201c8589b98e090f9 
Finally after some googling, I could solve the issue with the following command:
[administrator@bdsi989-linux administrator]$ rvm get head
Hope this helps some one :)

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