Nathan Leach
2006-Sep-21 19:13 UTC
Can I access an object attribute dynamically using a variable as the field name?
Not sure exactly how to put this, but I want to assign a value to an attribute of an object (e.g. part.partnumber) using a dynamic name for the attribute (a variable of type String). It seems like I should be able to, but I can''t seem to figure it out. Any help would be appreciated. Here''s some code to illustrate what I have tried so far... # Create a field name and find a part fieldname = "partnumber" part = Part.find(params[:id]) # Try to access an attribute using the dynamic field name # # I actually want an assignment, but logging is an adequate example # logger.info(part.fieldname) #NOPE logger.info(part[fieldname]) #NOPE logger.info(part["''" + fieldname + "''"]) #NOPE logger.info(part[":" + fieldname]) #NOPE --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Nathan Leach
2006-Sep-21 19:34 UTC
Re: Can I access an object attribute dynamically using a variable as the field name?
Found it... part.send(fieldname) -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com]On Behalf Of Nathan Leach Sent: Thursday, September 21, 2006 2:14 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Can I access an object attribute dynamically using a variable as the field name? Not sure exactly how to put this, but I want to assign a value to an attribute of an object (e.g. part.partnumber) using a dynamic name for the attribute (a variable of type String). It seems like I should be able to, but I can''t seem to figure it out. Any help would be appreciated. Here''s some code to illustrate what I have tried so far... # Create a field name and find a part fieldname = "partnumber" part = Part.find(params[:id]) # Try to access an attribute using the dynamic field name # # I actually want an assignment, but logging is an adequate example # logger.info(part.fieldname) #NOPE logger.info(part[fieldname]) #NOPE logger.info(part["''" + fieldname + "''"]) #NOPE logger.info(part[":" + fieldname]) #NOPE --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Ezra Zygmuntowicz
2006-Sep-21 20:05 UTC
Re: Can I access an object attribute dynamically using a variable as the field name?
On Sep 21, 2006, at 12:13 PM, Nathan Leach wrote:> Not sure exactly how to put this, but I want to assign a value to > an attribute of an object (e.g. part.partnumber) using a dynamic > name for the attribute (a variable of type String). It seems like > I should be able to, but I can''t seem to figure it out. Any help > would be appreciated. Here''s some code to illustrate what I have > tried so far... > > # Create a field name and find a part > fieldname = "partnumber" > part = Part.find(params[:id]) > > # Try to access an attribute using the dynamic field name > # > # I actually want an assignment, but logging is an adequate example > # > logger.info(part.fieldname) #NOPE > logger.info(part[fieldname]) #NOPE > logger.info(part["''" + fieldname + "''"]) #NOPE > logger.info(part[":" + fieldname]) #NOPEpart.send fieldname -Ezra --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---