Question on model and fields in form

Hi there,

Her is the thing.

Let's say, I have model "Phonebook", which has two fields "phone_number" and "full_name".

I also have view, with form which asks for "full_name" and (important here) couple of fields:

phone_int phone_area phone_number phone_extension

They all need to be concated like

phone_int (phone_area) phone_number ext. phone_extension


phone_int = +1 phone_area = 415 phone_number = 321-2345 phone_extension = 101

it need to become

phone_number = +1 (415) 321-2345 ext. 101

Do not ask me why the hell am I doing this thing, it's just an example, and yes, there are some legacy database structure cases where something like that has to be done :slight_smile:

So here in controller I am doing @phonebook.update_attributes(params[:phonebook]) and fields phone_int, phone_area, phone_number, phone_extension are defined with attr_accessor in the model.

So, where do I do the concatenation process? In the model? Do I do it before calling update_attributes in controller? What will be the best practice?

I see it can have it place in the model, but where should I put it exactly so it'll work out before update_attributes?


In the model do

before_save :concat_phonenumbers

def concat_phonenumbers   self.phone_number = "#{phone_int} " + "#{phone_area} " + "#{phone_number} " + "#{phone_extension}" end