Fetch data from heterogenous tables(Rails SQL views, SQL Union)

Let’s say you have a ‘cars’ table and a ‘bikes’ table, and the schema can not be changed. Some fields are the same, and you want to display ( paginate, search etc) a list with products that are both cars and bikes (from both the car and the bike models), sorted by their creation date.

This post is about how this can be accomplished using sql views.

Continue reading

Phone calling codes (aka International Dialing codes) – DB Dump / YML format

Parsed from: http://en.wikipedia.org/wiki/List_of_country_calling_codes with Hpricot:

doc = Hpricot(open("http://en.wikipedia.org/wiki/List_of_country_calling_codes"))
table = (doc/"table")[4]
by_country = {}
table.search('/tr').each {|tr| tr0 = tr.search('/td')[0] ; tr1 = tr.search('/td')[1]; by_country[tr0.inner_html.gsub(/<.*>/,  "")] = tr1.search('/a').inner_html.gsub(" ", '').split(/\+/).select{|val| !val.empty?}.map(&:to_i) if tr1  } ; nil

Phone calling codes (MySQL dump)

Phone calling codes by country (YML format)

Why You Shouldn’t Use Float for Currency (floating point issues – explained for Ruby and RoR)

This article is a response to https://vladzloteanu.wordpress.com/2010/01/06/ruby-on-rails-interview-questions-update/

It is a VERY BAD IDEEA to use floating point arithmetics to deal with currency. In most of the programming languages. Basically, because you’ll end up loosing money :). And this (on the great majority of cases) is not desirable 🙂 .

I’ll show you some magic (that you may try at home):
~$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
~$ irb
irb(main):003:0> (10.12*100).to_i
=>; 1011

So, what just happened? Continue reading

Ruby on Rails Interview questions – update

Most of Google searches that hit my blog are about ‘ruby on rails interview questions‘.

Responding to this popular request 😉 , I will post 3 more questions that are related.

1. What type of data would you use to represent currency?

2. Why is it not a good practice to use float for currency?

3. Explain the following behaviour:

~$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
~$ irb
irb(main):003:0> (10.12*100).to_i
=>; 1011

The answer – in my next post .

Rails Internationalization: currency format configuration

One of the nicest things in the built-in Rails internationalization features is the ability to format the currency according to the local culture (the number_to_currency method). For example, many european contries use ‘,’ as a separator and ‘.’ as a delimiter.

However, the examples presented in the help page (http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001684) are missing something essential.

 number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "")
 # => &pound;1234567890,50
 number_to_currency(1234567890.50, :unit => "&pound;", :separator => ",", :delimiter => "", :format => "%n %u")
 # => 1234567890,50 &pound;

Why should i need a helper method where i can set all those parameters by hand?

Digging in the source code, i found how those settings can be put in the locale yml config file. Below is an example from my french localization file.

        delimiter: ','
        separator: '.'
        precision: 2
        format: '%n %u'

VPS-like Virtualization sollution for Rails Servers: OpenVZ

This article shows how to create multiple VPS-es using OpenVZ and how to configure those VPS-es as a Rails prod/testing server.

It is an updated and improved version of http://wiki.openvz.org/Quick_installation

Why OpenVZ?

+ low overhead, good performace (claiming only 3% penalty)

+ soft memory allocation

+ free

+ relatively easy to install/manage

– requires both host and to guest OS be  Linux

– no GUI on guest machines

Continue reading

Rails IDEs issues: change Java interpretor to Sun Java 6 JRE

Sometimes,the Rails (mostly written in Java, like RadRails, RubyMine, 3rd Rail, or Netbeans for Ruby) have some problems running with the default Ubuntu open source java interpretors. You may check the installed java version with:

java -version

Continue reading

Complete Ubuntu install for a Ruby on Rails developer (utils, rails, ff addons)

This is a guide on creating a full-featured Ubuntu dev box for RoR. It assumes that you have already installed the latest version of Ubuntu.


sudo apt-get install build-essential
 Continue reading 

Rails Startup App – Basecamp (with Rails 2.3)

A lot of GitHub projects are dedicated to creating a Rails Start-up application that should have the most used functionalities in any web application, like:

  • user authentication
  • profile management
  • user roles
  • users management
  • integration with other authentication systems (OpenID, FacebookConnect, etc)

Some of the most well known are listed below:

Continue reading

Ruby on Rails interview questions – part 2

Continuing the “RoR interview questions” sequel(part one here) , these are some general Rails concepts and technologies questions. Enjoy :).

Continue reading