January 17, 2012

Note: Test and View available Rails Routes

Filed under: Uncategorized — Tags: , , — yasi8h @ 3:39 am

See all available routes

rake routes

Check route/path/URL helpers (in rails console)




June 30, 2011

Fixing foursquare2 gem: NameError: uninitialized constant MultiJson

Filed under: Uncategorized — Tags: , , , — yasi8h @ 12:00 pm

If you get a error similar to the one bellow when you try to use (perform a simple task such as get a venue) from the foursquare2 gem. You are probably missing the multi_json gem.

NameError: uninitialized constant MultiJson
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday_middleware-0.6.5/lib/faraday/response/parse_json.rb:16:in `parse’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/response.rb:17:in `on_complete’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/response.rb:9:in `block in call’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/response.rb:62:in `on_complete’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/response.rb:8:in `call’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/response.rb:8:in `call’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/adapter/net_http.rb:63:in `call’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/request.rb:88:in `run’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/request.rb:28:in `run’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/connection.rb:170:in `run_request’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/faraday-0.6.1/lib/faraday/connection.rb:64:in `get’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/foursquare2-1.1.0/lib/foursquare2/venues.rb:9:in `venue’
from (irb):6
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start’
from /Users/jamesx/.rvm/gems/ruby-1.9.2-p180@snipwits3/gems/railties-3.0.7/lib/rails/commands.rb:23:in `’
from script/rails:6:in `require’
from script/rails:6:in `’ruby-1.9.2-p180 :007 >

So to fix it, just add gem ‘multi_json’ to your Gemfile.

May 17, 2011

My RedDotRubyConf Experience

Filed under: Uncategorized — Tags: , — yasi8h @ 7:42 am


It have been sometime since i last blogged about anything. I have a long queue of things that i want to blog about in my head and in my todo lists. However i find my self tired and distracted to do any blogging most of the time. However today i am going to write about the RedDotRubyConf that was held in Singapore on April 22nd-23rd.

As a programmer and a technology enthuastist i have attended numerous gatherings of developers and like minded people. Back in SriLanka these meet-ups were mostly related to Linux or FOSS and organized by the [LK-LUG][10]. When i moved to Singapore sometime back (as my new position was located there) i started attending a JS meet-up ([beermates.js][11]) that was held in [][12]. However i have never attended a technology conference in my life before RedDotRubyConf.

My company ([favoritemedium][13]) sponsored the tickets(thanks! [@5ooyoung][14]). Me together with two other coworkers attended the conf. It was the first time ever a Ruby related conference was organized in the south east asian region (according to many people ;)).

Speakers and Talks

I thought this was the most important thing in a conference. For a conference to be good, it should have good speakers and good talks. However my thoughts about this changed after i attended the conference.

A lot of cool (influential, popular, rich(!), smart) (and not-so-cool => agile, agile, agile, pair programming, agile, agile… 😀 just kidding!) speakers gave talks in the con(short for conference from here on). I think as a whole they were grate. You can see a full list of the Talks and the Speakers here. I am going to write about the talks that got most of my attention/’mind share’.

**These Talks Inspired Me**
– Mikel Lindsaar – Ruby can haz everyone
– Matz – The future of ruby
– Keynote – Dave Thomas, Pragmatic Programmers
– Advanced git techniques – Tom Preston-Werner, Github
– How to be awesome at rails – Ryan Bigg, RubyX

**These were Interesting**
– Agile the pivotal way – Ian McFarland, Pivotal Labs
– Deploying infrastructure with opscode chef – Robert Berger, Runa
– Houseparty, slumlord, or mogul? multi-tenancy with rails – Paul Gallagher, Evendis
– Agile in Real Life – Andras Kristof, ViKi
– Building a financial app in rails – Matt Jacobs, Thoughtsauce
– Flexible & adaptable to change: mongodb – Alex Nguyen, Vinova

I am not the kind of person who tries to learn a lot of new things from a event like this. I feel that if i have enough inspiration and desire i could learn about tech/framework/lang… X in my free time using the resources in the internet and all the helpful people that i can contact. However inspiration is not easy to come by, this event inspired me a hell of a lot!. Thats what its all about. I saw grate people who had awesome careers and contributions to the open source community. Seeing folks like that inspire me to try and do more!

The Crowd

I think the best part of the con was the awesome crowd. They were diverse and they were from different parts of the world. I met people from Singapore, Philippines, India, China, Indonesia and the US. But there were people from all over the world, specially from south east asia and Australia.

Most people i talked to were developers and most others were founders. I see this trend in lot of events. Developers attend events because they are about interesting topics and because they can meet interesting people. Founders attend events to mainly meet interesting people. Who knows may be you can meet your partner in some conference if you were a founder searching for one!

I regret that i didn’t have my ‘business cards’ ready. A lot of other people did though. I met folks from pretty interesting companies in the tech world today. I think events like these are grate to make new contacts and basically get to know interesting hackers. The more hackers that i talk to, the more inspiration i get, to be better at what i does. I felt like i have been waisting my time and energy doing nothing.

Pivotal Labs, ViKi, RubyX, Github and Others

I am grateful that the guys mentioned above and others who sponsored the conf and made it happen. I hope they got back some value out of the event. Most of the companies above talked about how they do things. As in how their developers develop software and spend their days. About doing things the agile way and pair programming. It was very interesting. I think the hacker inside me like how Github works. They seemed to be the most liberal out of the lot. However Github is a special company. So what works for them might not work for everyone else. The most interesting thing to me about Pivotal Labs was the fact that ‘all their developers goes home at 6pm’! Man if this is true… its a huge plus for folks who are working there, i guess.

The Venue and Free flowing sources of Caffeine

The conf was held in the Singapore Management University. I was pretty satisfied with the facilities provided at the venue. However it would have been grate if the corridors were air conditioned. As we spent a significant amount of time talking to folks outside.

Inside the auditorium, they were two screens which mirrored each other. This confused me sometimes. It made it hard to follow the slides as i always tend to scan both screens hoping to find something different. I think its something thats ‘wired’ in to my brain.

I have to say that the coffee was GOOD. I am not a ‘coffee person’ so to say. However the coffee at the conf was good, it had the taste and it had the kick ;). I even had some fun figuring out how to use the coffee machines (never use them before, interesting stuff). There was RedBull, Coke, Water other than coffee. And there was a lot of it almost all the time. Well done organizers! The lunch was pretty good too. I have no complaints what so ever about ‘them’ food.


I am so happy that i got a ton of stickers specially the Github ones 😉

2011 04 23 23 52 14

…and a neat (target-the-red-dot) tee! 🙂


Obviously enough, i wasn’t the only one to write about this. Here is a list some interesting links relating to the conf.

– [The Official Website][4]
– [A Comprehensive list of links by @cheeaun][2]
– [Google Group/Newsgroup for Attendees][3]
– [A Photo set on fliker][1]

I am eagerly waiting for the next RedDotRubyConf, hope to see you there!


February 14, 2011

Fixing: /irb/completion.rb:9:in `require’: no such file to load — readline (LoadError) on rvm

Filed under: Uncategorized — Tags: , , , , , — yasi8h @ 10:25 am

Got this error to in one of the staging servers:

$ rails c production

/home/jooolaesa/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/completion.rb:9:in `require’: no such file to load — readline (LoadError)
from /home/jooolaesa/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/completion.rb:9:in `’
from /home/jooolaesa/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.0/lib/rails/commands/console.rb:3:in `require’
from /home/jooolaesa/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.0/lib/rails/commands/console.rb:3:in `’
from /home/jooolaesa/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.0/lib/rails/commands.rb:20:in `require’
from /home/jooolaesa/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.0/lib/rails/commands.rb:20:in `’
from script/rails:6:in `require’
from script/rails:6:in `’

It will appear if your ruby installation is not compiled with readline library. The solution is to recompile it with readline. If you are using RVM to manage your ruby installations you would go about doing it like this.

rvm package install readline

rvm remove 1.9.2 # note that 1.9.2 must be replaced with whatever ruby installation that you are going to recompile with readline support

rvm install 1.9.2 –with-readline-dir=$rvm_path/usr

Thats it!


February 7, 2011

Find all (locally and remotely) available versions of a ruby gem

Filed under: Uncategorized — Tags: , — yasi8h @ 12:29 pm

This one took about 10 mins for me to find out. Shame oh shame…

If you are looking for all versions of rails thats been released and can be installed as a gem:

gem list rails -all -r

be warned that this will list all gems that have the word rails in it and all versions of them! just filter it out.

January 27, 2011

Switching Devise Mail Sender Address with the Environment

Filed under: Uncategorized — Tags: , , — yasi8h @ 7:04 am

This is something very simple that i took more than 5 minutes to find out 🙂

in file: config/initializers/devise.rb

if Rails.env.development?
config.mailer_sender = “”
elsif Rails.env.production?
config.mailer_sender = “”

Rails.env does the trick.

November 19, 2010

Rails Authorization wiht CanCan

Filed under: Uncategorized — Tags: , , — yasi8h @ 5:59 am


There are many authentication frameworks for rails. And a lot of people do use for getting basic authentication working in their apps. However most people tend to implement their own thing when it comes to authorization. I think this is because most people feel that their authorization needs are pretty simple and hence need no plugins. But when an application grows these home cooked solution may not scale well. After all why reinvent the wheel? (well i am sure that there is a time and a place where reinventing wheel is appropriate).

CanCan is a authorization framework that aims to be simple and provide you a central place to manage all your authentication logic. There are however more complex authorization frameworks out there. So if you do feel that CanCan doesn’t give you all the control you need you can do some googling.

You can get CanCan from

Defining Authorization Logic

What i like the most about CanCan is its ability to define all the authentication logic in one single class. This is called Ability.

The following is an example from ASCII Casts episode 192 about CanCan. (Yes there are some railscasts and some asciicasts about CanCan).

class Ability
include CanCan::Ability

def initialize(user)
can :read, :all

A bit more complicated example from one of the projects that i am working on right now looks like this.

class Ability
include CanCan::Ability

def initialize(user)
if user && user.role == ‘Admin’
can :manage, :all
elsif user && user.role == ‘Producer’
#can :read, :all

can [:index, :new, :create], User # can view existing users and add new users to the system

can [:show, :edit, :update, :destroy], User do |user_o| # can edit/destroy any users/producers who are associated to the current_producer’s publications
user.users_related_to_my_active_publications([“Producer”, “User”]).include? user_o

# Catch all block that authorize doing anything on the rest of the Classes
#can :manage, User
can :manage, Asset

elsif user && user.role == ‘User’
#can :read, :all
can :index, User #as the root controller of the application is set to UsersController we need to let every role into UsersController’s index action.
#however this does not mean everyone can see a list of users. Non admins will be redirected.
can [:show, :update], User do |user_o| #let users update and view their own profiles, but not others ==

# Catch all block that authorize doing anything on the rest of the Classes
can :manage, Asset
can :manage, Audio
can :read, :all

CanCan is mostly independent from any authentication framework that you may use. It is not tightly coupled with any specific frameworks. But it does depend on the controllers having a method called ‘current_user’. This method should give the currently logged in user. So when CanCan wants to authorize some action it will grab the output from current_user and pass it in to the initialize method of the ability class. Inside the initialize method you put your custom logic and tell CanCan what and what not a certain role can do. This is accomplished by using the can method. With the can method you can define access levels for user roles per models.

When you say can:read, Asset. It means that the user role concerned can access controller actions that allows reading Assets. For example listing all the assets or viewing a asset through the show action. So in any can :x, :y statement, the x tells cancan what actions should be allowed and the y tell cancan for what objects (Users, Books…etc) the x actions are allowed for.

The actions available are what you have in any scaffold generated restful controller. However for ease cancan have grouped some of them in to some ‘actions’.

def default_alias_actions
:read => [:index, :show],
:create => [:new],
:update => [:edit],

The above method is from cancan’s source code and it defines the default alias for actions. So if you pass in :read it would mean that you are authorizing the index and show actions. The :manage action would basically authorize all actions.

If you want to check for custom actions you could just use those instead of the standard rest actions. For more info look at (

You can pass in conditions to the can statements as follows (from

can :read, Project, :active => true, :user_id =>

The conditions passed in as a hash in the third argument are tested against the user object on which the authorization is performed on.

Or as a code block.

can [:show, :update], User do |user_o| #let users update and view their own profiles, but not others ==

I want to let user’s update their own profiles but not others. So i have added a small check for that inside little block that is passed in as a argument to the can method.

Now you know how to write some basic authorization rules. But before cancan can get to work. It need to be ‘plugged in’ to your controllers. So we have to call the method


in any controller on which we want out authorization rules applied. This method call will basically add a before filter that invokes the authorization. Or else you can put the authorization logic in separate controller actions, like:

def show
@article = Article.find(params[:id])
authorize! :read, @article

The above code (from would check for the given ‘abilities’ for the current_user and throw a exception if the current_user does not have needed authorization.

You can also check the authorization logic in a adhoc manner.

<% if can? :update, @issue %>
<%= link_to “Edit Issue”, edit_issue_path(@issue) %>
<% end %>

The above code would check whether the current user have the right to access the edit action of the articles controller.

Handling Authorization Exceptions

When a user tries to do something that s/he is not authorized to (and given that you are invoking the correct cancan methods to check for authorization) cancan will throw a cancan access denied exception. This generates a ugly error message. So you can handle this in way you want by catching it like the following code (from demonstrates.

class ApplicationController < ActionController::Base
rescue_from CanCan::AccessDenied do |exception|
flash[:alert] = exception.message
redirect_to root_url

Authorization when fetching ActiveRecord records

CanCan does support doing authorization when retrieving records, however this have some limitations. For more info look at

Drawbacks / Shortcomings

CanCan is a neat little gem. However it have its pros and cons. As for the cons i feel that its more easily used with restful controllers and not so easily with none restful controllers. This is not to say that you can’t use it with non restful controllers. I think CanCan is better suited for instances where you have some straight forward authorization work. As i have mentioned earlier in the article cancan tries to be simple and not too overly complicated. So it doesn’t have a lot of fancy features. But it does what it promises to do, and its pretty neat at it!.

Ubuntu 10.04, Nginx with Upload and Upload Progress Modules, RVM, Postgresql

Filed under: Uncategorized — Tags: , , — yasi8h @ 2:02 am

Had to setup a staging server recently. Thought of putting down the steps i followed in to a article. This thing is getting fun.

General Stuff

We need these libraries in order to compile the following stuff

sudo apt-get install build-essential libssl-dev libpcre3-dev git-core

These are some nice to have tools, install only if you use them.

sudo aptitude install htop

You might also want to run update-db so that locate would work.

sudo update-db

Nginx from Source

We are going to get nginx 0.7.65 with upload and upload progress modules and compile them from source. Please note that additional command line options passed in to some commands bellow via two ‘-‘ character might get jumbled up by blog/editor. So if you get any unusual errors while running the following commands check whether you are typing in the correct chars (if you do a copy paste you might be trying to execute some html chars that make no sense for the command you are running).

tar xzf nginx-0.7.65.tar.gz

wget –no-check-certificate
tar zxf masterzen-nginx-upload-progress-module-v0.8.2-0-g8b55a34.tar.gz

tar zxf nginx_upload_module-2.2.0.tar.gz

cd nginx-0.7.65
./configure –add-module=../masterzen-nginx-upload-progress-module-8b55a34/ –add-module=../nginx_upload_module-2.2.0/
sudo make install

Ruby 1.8.7 with RVM

bash < <( curl )
nano ~/.bashrc
source ~/.rvm/scripts/rvm
rvm install 1.8.7
rvm use 1.8.7 –default

Rails 2.3.5

We want to install an older version of rails so

gem install rails -v 2.3.5

Postgresql 8.4

sudo apt-get install postgresql-8.4
sudo nano /etc/postgresql/8.4/main/pg_hba.conf

Towards the end of the file look for a line like

local all postgres ident

and replace it with this line

local all all trust

The above line would allow full access to the db from the localhost WITHOUT any PASSWORD. So yeah be sure you know what you are doing. You could replace trust keyword with the md5 to enable password authentication.

sudo /etc/init.d/postgresql restart

Postgresql installation have already added a user called postgres. And this user can can connect to the db and have admin rights. So connect as postgres and add a user account and create a db. Be sure to give all the needed privileges to the user account you are creating. So that it can create new dbs…etc.

createuser builder -U postgres
createdb builder_development -U builder

Ruby Gems needed for postgresql – ruby integration

sudo apt-get install postgresql-server-dev-8.4
gem install pg

Thats it for me. Hope this helps.

November 10, 2010

Retrieving DISTINCT results with Active Record on Complex Queries

Filed under: Uncategorized — Tags: — yasi8h @ 8:53 am

If you want get distinct results with Active Record, there are some ways to go about it. The simplest is to just use SQL (ie: by using find_by_sql…).

But if you have a query like the following and do not want to switch to pure SQL. You could try the following.


PublicationsUsers – This helps maintain a ‘Users can have one or more Publications’ relationship between Publications and Users.

The following query gets all the users associated with all the publications of a given user.

@my_publication_ids is a array of publication ids like [0,5,1,21] and role is a string like ‘Admin’.

But the following statement will give you duplicate results. As in the collection returned can include the same user more than once. This is not the expected behavior.

User.all(:joins => :publications, :conditions => {:publications_users => {:publication_id => @my_publication_ids}, :users => {:role => role}})

Adding a :select => ‘DISTINCT “users”.*’ clause will solve the problem. Here note that we are interested in retrieving distinct users. Hence we have to apply the select distinct on users columns. Which looks like “users”.*

User.all(:select => ‘DISTINCT “users”.*’, :joins => :publications, :conditions => {:publications_users => {:publication_id => @my_publication_ids}, :users => {:role => role}})

October 24, 2010

Rails 3 on Ubuntu 10.10 with RVM, Passenger and Nginx

Filed under: Uncategorized — Tags: , , — yasi8h @ 1:09 am

This covers setting up a Ubuntu 10.10 box to run rails 3 on production. Please notice it only focus on installing and setting up things mentioned in the title. However there are other things that you should configure and setup when deploying a application to production (like ntpd for example).

Installing RVM

We will need git before we can get started.

sudo apt-get install git

Install rvm according to the official guide.

bash < <( curl )


[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.

to your .bashrc.

Once you edit your bashrc file execute the following to load rvm without logging out and back in

source ~/.rvm/scripts/rvm

Just to be safe check wether rvm is a function, which is what it should be.

type rvm | head -n1

Install ruby 1.9.2

See what you get out of rvm notes. you will need to install some stuff before you can compile ruby.

aptitude install build-essential bison openssl libreadline5 libreadline5-dev curl git-core zlib1g zlib1g-dev libssl-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libmysqlclient-dev

The above packages covers some common stuff. It should be enough to get you started. However depending on what gems you want to install you might need to install additional dependencies.

rvm install 1.9.2

Set 1.9.2 as the default

rvm use 1.9.2 --default


Install passenger as instructed in rvm documentation

rvm 1.9.2 --passenger
rvm 1.9.2
gem install passenger
rvmsudo passenger-install-nginx-module

Let passenger downland and install nginx for you.

Modify /opt/nginx/conf/nginx.conf (thats the default location).

‘For Nginx users, replace the passenger_ruby line with:’

passenger_ruby /home/wayne/.rvm/bin/passenger_ruby;

Add a block like this to host your RoR site

server {
      listen 80; #the server will be running on this port
      root /home/deployer/your_rails_project/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;

Nginx init script


git clone git://
sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
sudo chown root:root /etc/init.d/nginx

More information on


sudo apt-get install mysql-client mysql-server

Get the source for your rails app. unzip it in your home folder (or where ever you want to. But be sure to edit the nginx config as appropriate).

cd to-your-rails-app-folder
bundle install
rake db:setup RAILS_ENV=production

sudo /etc/init.d/nginx restart

Thats it!

Older Posts »

Blog at