, , , , ,

For all, who have started learning RoR and for those who are into this, here are some of the facts and conventions that you can follow, to make your Ruby code more shorter and easy to understand.

Let’s look at it how:

1. This is how a beginner writes:
if params[:email]
@email_add = params[:email]
@email_add = “Some Default Address”

You should write in 1 line, let’s see how:
@email_add = params[:email] ? params[:email] : “Some Default Address”

2. How to Improve the performance of any method inside your controller, if it is taking much time rather than the expecting.

Consider a scenario where we have one method, like:
def index
@users = User.find(:all)
@members = Member.find(:all)
@last_year_users = User.find(:all, :conditions => [“Some condition”])

Now here suppose these queries returns data in a heavy size, definitely it will gonna take time to load, so how we will improve the performance in this case?

3 How to update ruby 1.8.6 to 1.8.7 or 1.9?
This makes me frustrated most of the times. I am having Ruby 1.8.6 as a stable version on my system. Now I want to update it to 1.8.7 so that I can sense Rails 3.0 and its features, but most of the sites and blogs are giving instructions to install it as a fresh copy and most of the sites are giving information that 1.8.7 is not compatible with rails 2.1 or higher.

So If someone knows how to upgrade, so that It won’t affect my existing apps running on 1.8.6 and it will install both copies of Ruby like rails does, please do post it here. I’ll be very thankful

4. Using polymorphic and as associations with models
Now, for most of the programmers, consider a scenario where multiple models has 1 to N relationship with one model. To make it more easier lets take 1 example.

Consider the following diagram:

Where models like School, College, Event and Semester are having multiple relationship with a single model i.e Student. Do you think, our student model will have foreign keys like school_id, college_id, event_id and semester_id? Will it be a good idea to have multiple foreign keys into one table?
No it doesn’t make any sense. So whenever you have a scenario like this, you have to follow polymorphic and as associations. How? Let’s see:
In Your models i.e school.rb, college.rb, semester.rb and event.rb, define something like this:

class School < ActiveRecord::Base
has_many :sc_students, :class_name => “Student”, :as => :rollable, :dependent => :destroy

class College < ActiveRecord::Base
has_many :col_students, :class_name => “Student”, :as => :rollable, :dependent => :destroy

class Semester < ActiveRecord::Base
has_many :sem_students, :class_name => “Student”, :as => :rollable, :dependent => :destroy

class Event < ActiveRecord::Base
has_many :ev_students, :class_name => “Student”, :as => :rollable, :dependent => :destroy

class Student < ActiveRecord::Base
belongs_to :rollable, :polymorphic => true

So, what is the advantage here to use as association and polymorphic is, you don’t need to create school_id, college_id, semester_id and event_id columns in the students table. So when you create migration, you’ll have to create two columns i.e.

t.integer :rollable_id
t.string :rollable_type

So for example colleges the entry will have rollable_id as specific college id and rollable_type as “College”. How simple and short implementation, isn’t it?