Topic: How to use a variable in controller defined in the model

I have a variable (real_number) which is also a column in the table (subreqs) and it's defined in the subreq.rb as:

def real_number
   [br_tr_no,sub_function,scope_id,req_extension].reject(&:blank?).join('.')
end

When I try to use it in the controller to set the value, it doesn't recognise "real_number"
@subreq.real_number = real_number

Re: How to use a variable in controller defined in the model

You need to call @subreq.real_number in the controller.

@subreq.real_number = @subreq.real_number

But it's much better to place this in a before_save callback:

def before_save
   self.real_number = [br_tr_no,sub_function,scope_id,req_extension].reject(&:blank?).join('.')
end

Then you don't need to set anything in the controller, it will automatically be set when it saves.

Railscasts - Free Ruby on Rails Screencasts

Re: How to use a variable in controller defined in the model

Thanks Ryan...It works.