Wednesday, December 31

Qucik Fix: ActiveRecord Migrations reset_column_information

Few days back I had created few migrations that would rename existing table and again create a table with old name.
  1. user should be renamed as "old_users" 
  2. create a new table "user"
  3. restore the data with different column names in the "user"

  def self.up
    rename_table :users, :old_users
    create_table :users do |t|
      t.column :created_at, :datetime
    end  

    Temp.find(:all).each do |user|
      new_user = User.new(:id=> user.id, :created_at => user.created_at)
      new_user.save!
    end
  end

Don't worry about the Temp, Temp is a class through which I am accessing the old_users table.  Precisely,

class Temp <>
  set_table_name "old_users"
end

This worked pretty well in my local environment and when I deployed in staging, it crashed.  The error was that, it was trying to insert data into table with prvious structure/columns.  Which means, the new structure hasn't taken effective.

The quick fix seems to be reset_column_information.  Before pushing/creating fresh data in the table, I need to reset the column information of the tables.  So including the following line:
      User.reset_column_information
right before creating new users solved the problem.

By the way "WISH YOU HAPPY AND PROSPEROUS NEW YEAR"

Quick Fix: YAML syntax & Fixtures issue

Today I got a mail from one of my juniors who was struck with some strange problem.  
While he started playing with built in unit tests in one of his rails project, he got an error

Errno::ENOENT: No such file or directory - ramesh

  I was bit confused with this error and asked him to send me a screen shot of the error so that I can understand it better.  And here he sent the following snapshot,

Well when I got this it was very clear that the problem was with fixtures.  So I peeked into his fixtures, which is like this:


having looked into the fixture,  it was pretty clear that this is about right syntax.  And this was the quick fix which worked out:


Quickly revising back about YAML & Syntax while defining hierarchical elements would give you clear view of this quick fix.

  Anyways, it was the mistake in the indentation while defining the YAML :)

Tuesday, December 30

Usability Tip: Ajax loading/busy indicator

What is Ajax?
In a nutshell, "With Ajax,web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page".

  Now that these requests are happening at the background, user should be aware that the application is trying hard to retrieve the data.  But, How do we do that?

This is where we can use Ajax loading/busy indicator.  We show some text/image saying that we are working/loading... until the request is completed.  This would increase the usability and end user will be happy enough.  
  I am going to show it's implementation in RoR based web applications.  

Module ActionView::Helpers::PrototypeHelper, takes care of Ajax implementation through prototype library.





as you see the rails form tag helper comes out with interesting usability options. Above we just implemented, :loading & :complete options where-in it would show/hide particular div ("loading"). And what's in that div?




well it's done.  
Few samples...




Do I need a designer? 
hmm... we don't need designer for every thing. Especially this usability can be entirely handled by developer.  

How do I create an image? I don't have prior experience! 
Not needed just follow 3 simple steps 
2. select Indicator type, colors. 
3. Generate & Download It  

I don't know RoR, how can I do it in PHP/Python/Java/JavaScript? 
This needs prototype, JavaScript library that provides DOM manipulation. The same is achieved like this:



use the above snippet.

Thursday, December 18

Tweak: updating ruby gems

Recently was helping my friend to setup a twitter based application on server.  And gems needed to be updated as ruby gem "twitter" needs rubygems of  >=1.2.0
  And as usual passed the update query and after a few minutes there is an interesting message saying "nothing to update". 
  There seems to be a minor tweak to achieve this.  A two step tweak which needs another gem installed and then update.
  1. gem install rubygems-update
  2. update_rubygems
and that does the trick ;)

Monday, December 8

Range Class in Ruby

"Range" is an interval with starting and ending values. In ruby ranges can be created either by "literals" or directly through the "Range::new". And of the literals, x..y & x...y. The main difference of the two mentioned literals, inclusion/exclusion of end values in the range.
x..y: includes the end values

(1..5).each do n
puts n, ' '
end


(1...5).each do n
puts n, ' '
end
In the first literal case, values 1, 2, 3, 4, 5 would be printed. Whereas, in the second litereral case 1, 2, 3, 4 only will be printed.

Interestingly methods end/alias would still give the end value of the range. In such case, end_excluded? is apt choice to distinguish.