Rails Migrations and My first rails app

I’ve starting playing around with rails these couple of days. I don’t really have a programming background but hey its never to late to innovate yourself.

I was talking to the HR Manager today, and he mentioned that government required a “specially formatted” list in order for the company to keep their files order. Basically his staff are looking at an excel sheet with a list of 500 entries. Each entry contains a Full Name, Civil ID Number, Bank Account Number.

Now the government requires that list but without the Full Name… easy you say eh? But lets take a look at “how” they want it presented.

EMPCODE2-BankCode-CivilID-Account_number

And example of a properly formated entry would be:221234567890120001234567890123

EmpCode: 2 signifies this entry represents an employee (value of 1 would mean the employer)
Bank Code 2 = NBK (National Bank of Kuwait)
CivilID: a 12 digit Civil ID serial number (Found on your Civil ID Card)
Account Number: In our case all our employees bank with NBK, however NBK account numbers are 13 digits and the government needs 16 digits. So we have to add three zeroes as a prefix to meet their criteria

Now imagine what would happen if you had to create this text file according to the Ministries standard by hand… yep exactly a secretaries nightmare. Its also prone to Human Error. So I thought to myself this would be the perfect 1st mini rails project that I could use to learn Ruby on Rails and how to apply it to “Real World Problems”. I know its rather simple, but hell so far Im having a blast with it. The way Im doing it, might not be the best way, but heck Im learning, and I’m just doing this for fun at this point.

So the 1st step would be to create this application

rails civ_demo

I called it civ_demo for lack of a better name, and now I’m gonna cheat a bit, cause at this stage I just want to learn how to use validations. So Im going to use some scaffold magic. Since essentially this will be a CRUD (Create,Read,Update,Delete) web app.

script/generate scaffold Employee first_name:string middle_name:string last_name:string bank_account:integer civil_id:integer position:integer created_at:datetime updated_at:datetime

Ok so far so good, lets start this baby up.

script/server

Voila the basics are in place. So right away I’ve learned that the best place to put data validations is in the model.

# Required Fields
validates_presence_of   :first_name, :middle_name, :last_name, :bank_account ,:civil_id

# Value Validation

validates_length_of :first_name, :maximum=>30, :allow_blank => false, :message=>”First Name has to be less than {{count}} if you don’t mind”
validates_length_of :last_name, :maximum=>30, :message=>”Last Name has to be less than {{count}} if you don’t mind”

validates_length_of :bank_account, :is =>13, :message => “Bank Account number must be {{count}} digits.”
validates_length_of :civil_id, :is => 3, :allow_blank => false, :allow_nil => false

#Force user to use only numbers in the following fields (though they will be treated as a string when saved to the DB)
validates_numericality_of :bank_account, :only_integer => :true
validates_numericality_of :civil_id, :only_integer => :true

All this is good, however for some odd reason 888.length does not work, nor does validates_length_of. After some research I figured out then when dealing with numbers validates_length_of measures “byte size” and not how many digits the value has. Apparently it would have been better to use type string for the variables bank_account, and civil_id. That way “String”.length would return 6 hence the validation would work.

Problem is how do I change the database schema without writing complex migration files, or starting from scratch.

Solution: http://github.com/pjhyett/auto_migrations/

Basically if you install the auto_migrations plugin, you can directly edit the schema.rb file and edit say t.integer  “bank_account” to t.string  “bank_account”.
Then you just say rake db:auto:migrate, and voila your done! (Also your data validations work now too)

You gotta love rails