Hi I want to create a model attribute that looks exactly like a db field but is infact the result of a model function. Example I have a user with name and surname. I want to be able to do @user.fullname which will then call a function that returns "name surname" as the full name, and I would like to be able to pass "name fullname" and have it broken into the seperate fields before saving to the db, in otherwords I would like it to function exacly asif there is a fullname field. I know this can be done, I just dont know where to start looking for this. Thanks in advance Ivor --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Just define a method on the model like so: def fullname "#{self.name} #{self.surname}" end def fullname=(value) value = value.split self.name = value[0] self.surname = value[1..value.size] # in the case of long last names like van Hook end --Jeremy On 3/19/07, Ivor Paul <ivorpaul-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > I want to create a model attribute that looks exactly like a db field but is > infact the result of a model function. > > Example > I have a user with name and surname. > > I want to be able to do @user.fullname which will then call a function that > returns "name surname" as the full name, and I would like to be able to pass > "name fullname" and have it broken into the seperate fields before saving to > the db, in otherwords I would like it to function exacly asif there is a > fullname field. > > I know this can be done, I just dont know where to start looking for this. > > Thanks in advance > Ivor > > > >-- http://www.jeremymcanally.com/ My free Ruby e-book: http://www.humblelittlerubybook.com/book/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
awesine jeremy. thanks a lot. I am going to be doing a fair amount of those. so basically you write 2 functions, one looks like a setter because of the "=" sign at the end... very nice. cheers ivor On 3/19/07, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Just define a method on the model like so: > > def fullname > "#{self.name} #{self.surname}" > end > > def fullname=(value) > value = value.split > self.name = value[0] > self.surname = value[1..value.size] # in the case of long last > names like van Hook > end > > --Jeremy > > On 3/19/07, Ivor Paul <ivorpaul-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi > > > > I want to create a model attribute that looks exactly like a db field > but is > > infact the result of a model function. > > > > Example > > I have a user with name and surname. > > > > I want to be able to do @user.fullname which will then call a function > that > > returns "name surname" as the full name, and I would like to be able to > pass > > "name fullname" and have it broken into the seperate fields before > saving to > > the db, in otherwords I would like it to function exacly asif there is a > > fullname field. > > > > I know this can be done, I just dont know where to start looking > for this. > > > > Thanks in advance > > Ivor > > > > > > > > > > -- > http://www.jeremymcanally.com/ > > My free Ruby e-book: > http://www.humblelittlerubybook.com/book/ > > My blogs: > http://www.mrneighborly.com/ > http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---
awesome that is :) On 3/19/07, Ivor Paul <ivorpaul-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > awesine jeremy. > > thanks a lot. I am going to be doing a fair amount of those. so basically > you write 2 functions, one looks like a setter because of the "=" sign at > the end... > > very nice. > > cheers > ivor > > On 3/19/07, Jeremy McAnally <jeremymcanally-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Just define a method on the model like so: > > > > def fullname > > "#{self.name} #{self.surname}" > > end > > > > def fullname=(value) > > value = value.split > > self.name = value[0] > > self.surname = value[1..value.size] # in the case of long last > > names like van Hook > > end > > > > --Jeremy > > > > On 3/19/07, Ivor Paul <ivorpaul-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > wrote: > > > Hi > > > > > > I want to create a model attribute that looks exactly like a db field > > but is > > > infact the result of a model function. > > > > > > Example > > > I have a user with name and surname. > > > > > > I want to be able to do @user.fullname which will then call a function > > that > > > returns "name surname" as the full name, and I would like to be able > > to pass > > > "name fullname" and have it broken into the seperate fields before > > saving to > > > the db, in otherwords I would like it to function exacly asif there is > > a > > > fullname field. > > > > > > I know this can be done, I just dont know where to start looking > > for this. > > > > > > Thanks in advance > > > Ivor > > > > > > > > > > > > > > > > -- > > http://www.jeremymcanally.com/ > > > > My free Ruby e-book: > > http://www.humblelittlerubybook.com/book/ > > > > My blogs: > > http://www.mrneighborly.com/ > > http://www.rubyinpractice.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 -~----------~----~----~----~------~----~------~--~---