Topic: Checking null values and eliminating them

In my model, I'm concatenating some fields which is successful, but I want to eliminate the null values while concatenating. Don't know if "unless" should be used here:

def real_num
  "#{br_tr_no}" + "." + "#{sub_function}" + "." + "#{scope_id}"
end

Please do let me know if anyone has done the same.

Re: Checking null values and eliminating them

Placing them in an array and joining that is an excellent way to do this.

def real_num
  [br_tr_no, sub_function, scope_id].compact.join('.')
end

The "compact" method will remove any "nil" values in the array before joining.

Railscasts - Free Ruby on Rails Screencasts