Forum: Ruby on Rails Question on model and fields in form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6bd6f02e40540b5bfea5d85b7bd33e7c?d=identicon&s=25 Olexiy Prokhorenko (inrila)
on 2008-10-18 06:39
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

i.e.

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 :-)

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?

Thanks!
4c438a80fc30661ad619ea177cf9cbd0?d=identicon&s=25 Freddy Andersen (Guest)
on 2008-10-18 10:03
(Received via mailing list)
In the model do

before_save :concat_phonenumbers

def concat_phonenumbers
  self.phone_number = "#{phone_int} " + "#{phone_area} " +
"#{phone_number} " + "#{phone_extension}"
end
This topic is locked and can not be replied to.