I''ve got a phone number field defined in a model as a single String. However, the form used to input the phone number exposes the number as three distinct fields to help the user enter the number in the correct format. I''m mapping the three input parameters to the single string in a before_validation method which seems to work fine, but I want to be able to map the single string to the three distinct fields when the record is loaded from the database. What''s the correct hook to do that? I tried to provide a phone_number= method that used self[:phone_number]=... to assign the attribute and then split the phone number up into the three fields to be returned by accessors, but AR doesn''t seem to call this method when the record is loaded from the database. What''s the best way to accomplish this? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2008-Feb-01 22:10 UTC
Re: Mapping active record objects to virtual attributes
On Jan 31, 2008 3:10 PM, bigbanger <bigbanger10s-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''ve got a phone number field defined in a model as a single String. > However, the form used to input the phone number exposes the number as > three distinct fields to help the user enter the number in the correct > format. I''m mapping the three input parameters to the single string > in a before_validation method which seems to work fine, but I want to > be able to map the single string to the three distinct fields when the > record is loaded from the database. What''s the correct hook to do > that? I tried to provide a phone_number= method that used > self[:phone_number]=... to assign the attribute and then split the > phone number up into the three fields to be returned by accessors, but > AR doesn''t seem to call this method when the record is loaded from the > database. What''s the best way to accomplish this?class MyModel < ActiveRecord::Base ..... def after_find # call your method to split the phone number here end Another way to do this is with a virtual accessors def area_code phone_number[0..2] end def area_code=(area_code) raise ArgumentError.new(''invalid area code'') unless area_code.length == 3 phone_number[0..2] = area_code end of course this will differ depending just how you represent the numbers. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Eric Surname
2008-Jul-03 15:33 UTC
Re: Mapping active record objects to virtual attributes
Rick Denatale wrote:> On Jan 31, 2008 3:10 PM, bigbanger <bigbanger10s-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> phone number up into the three fields to be returned by accessors, but >> AR doesn''t seem to call this method when the record is loaded from the >> database. What''s the best way to accomplish this?I figured out how to use Aggregations to provide the necessary piecing and reassembly of phone number http://blogs.entertonement.com/nerdery/2008/07/rails-multipara.html -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---