If I were creating a Rails app from scratch I would use Rails''s naming conventions for fieldnames (such as first_name). However, I am doing a prototype for a legacy app that has fieldnames like FirstName and for other reasons need to keep them that way. Trust me on that. The validates_ series works fine with these legacy fieldnames, as do my views and controllers. But when I try to add some methods to my model that manipulate the names, Rails balks. For example, def full_name FirstName + '' '' + (if MI then MI + ''. '' else '''' end) + LastName end This is based on an example from Ruby For Rails (p. 84). Rails gives me "NameError in LoansController#list ... uninitialized constant FirstName ... This error occured while loading the following files: first_name.rb". I tried putting single quotes around ''FirstName'' (that turned it into a literal), using it as a symbol :FirstName, and putting brackets around it. I know the syntax itself is okay because if I substitute created_on.to_s for FirstName then that part is properly rendered by the view. I understand that Rails is supposed to favor convention but allow you to deviate if you must. So how do I refer to my nonstandard fieldnames within model methods? Thanks, Shauna -- 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 -~----------~----~----~----~------~----~------~--~---
Shauna wrote:> If I were creating a Rails app from scratch I would use Rails''s naming > conventions for fieldnames (such as first_name). However, I am doing a > prototype for a legacy app that has fieldnames like FirstName and for > other reasons need to keep them that way. Trust me on that. > > The validates_ series works fine with these legacy fieldnames, as do my > views and controllers. But when I try to add some methods to my model > that manipulate the names, Rails balks. For example, > def full_name > FirstName + '' '' + > (if MI then MI + ''. '' else '''' end) + > LastName > end > > This is based on an example from Ruby For Rails (p. 84). Rails gives me > "NameError in LoansController#list ... uninitialized constant FirstName > ... This error occured while loading the following files: > first_name.rb". > > I tried putting single quotes around ''FirstName'' (that turned it into a > literal), using it as a symbol :FirstName, and putting brackets around > it. I know the syntax itself is okay because if I substitute > created_on.to_s for FirstName then that part is properly rendered by the > view. > > I understand that Rails is supposed to favor convention but allow you to > deviate if you must. So how do I refer to my nonstandard fieldnames > within model methods?You can use either self.FirstName or self[:FirstName]. Or you can create a set of facade accessor methods, perhaps in a loop, using the underscore method to de-camelize every field name. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
This has more to do with Ruby syntax than with Rails per se. FirstCaps indicates constants. I don''t know what the "correct" response is to this, but one option is to use self.PropertyName instead...I did some testing and think that should work so long as the attributes are public Ethan Shauna wrote:> If I were creating a Rails app from scratch I would use Rails''s naming > conventions for fieldnames (such as first_name). However, I am doing a > prototype for a legacy app that has fieldnames like FirstName and for > other reasons need to keep them that way. Trust me on that. > > The validates_ series works fine with these legacy fieldnames, as do my > views and controllers. But when I try to add some methods to my model > that manipulate the names, Rails balks. For example, > def full_name > FirstName + '' '' + > (if MI then MI + ''. '' else '''' end) + > LastName > end > > This is based on an example from Ruby For Rails (p. 84). Rails gives me > "NameError in LoansController#list ... uninitialized constant FirstName > ... This error occured while loading the following files: > first_name.rb". > > I tried putting single quotes around ''FirstName'' (that turned it into a > literal), using it as a symbol :FirstName, and putting brackets around > it. I know the syntax itself is okay because if I substitute > created_on.to_s for FirstName then that part is properly rendered by the > view. > > I understand that Rails is supposed to favor convention but allow you to > deviate if you must. So how do I refer to my nonstandard fieldnames > within model methods? > > Thanks, > Shauna > > -- > 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 -~----------~----~----~----~------~----~------~--~---
self. worked great. Thanks for the help! Shauna -- 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Feb-28 11:49 UTC
Re: legacy fieldnames in model
Hi -- On Wed, 28 Feb 2007, Mark Reginald James wrote:> > Shauna wrote: >> If I were creating a Rails app from scratch I would use Rails''s naming >> conventions for fieldnames (such as first_name). However, I am doing a >> prototype for a legacy app that has fieldnames like FirstName and for >> other reasons need to keep them that way. Trust me on that. >> >> The validates_ series works fine with these legacy fieldnames, as do my >> views and controllers. But when I try to add some methods to my model >> that manipulate the names, Rails balks. For example, >> def full_name >> FirstName + '' '' + >> (if MI then MI + ''. '' else '''' end) + >> LastName >> end >> >> This is based on an example from Ruby For Rails (p. 84). Rails gives me >> "NameError in LoansController#list ... uninitialized constant FirstName >> ... This error occured while loading the following files: >> first_name.rb". >> >> I tried putting single quotes around ''FirstName'' (that turned it into a >> literal), using it as a symbol :FirstName, and putting brackets around >> it. I know the syntax itself is okay because if I substitute >> created_on.to_s for FirstName then that part is properly rendered by the >> view. >> >> I understand that Rails is supposed to favor convention but allow you to >> deviate if you must. So how do I refer to my nonstandard fieldnames >> within model methods? > > You can use either self.FirstName or self[:FirstName]. > > Or you can create a set of facade accessor methods, perhaps in a > loop, using the underscore method to de-camelize every field name.Another choice is: FirstName() which will force interpretation of FirstName as a method name. (I prefer self.FirstName -- I hate empty parentheses -- but I just thought I''d mention this one too.) David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---