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