Hi I have an object "a_row" with columns first, last, second to retrieve the value first I can do a_row.first but I need to do it a different way like this a_row.get_value_for_colum("first") and in the object class I would have def get_value_for_column(attribute) return attribute end but clearly i have an error attribute variable or method not defined. or I was thinking of doing self.#{attribute} but big error ***** I wanted to know if it is possible to achieve what I would like to do as the first solution is impossible to do for various reasons **** Thank You -- Posted via http://www.ruby-forum.com/.
On May 5, 4:53 pm, Anthony Ward <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > ***** I wanted to know if it is possible to achieve what I would like > to do > as the first solution is impossible to do for various reasons > ****well there''s always read_attribute (and remember that foo.read_attribute(:bar) is the same as foo[:bar] ) Fred> > Thank You > -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung wrote:> On May 5, 4:53�pm, Anthony Ward <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> >> ***** �I wanted to know if it is possible to achieve what I would like >> to do >> � � � �as the first solution is impossible to do for various reasons >> **** > > well there''s always read_attribute (and remember that > foo.read_attribute(:bar) is the same as foo[:bar] ) > > FredHi, thank you, just discovered read_attribute a few minutes ago!! again thank you -- Posted via http://www.ruby-forum.com/.
I think rather than doing read_attribute, you should do def get_value_for_column(attribute) send(attribute) end because if you have def something read_attribute(:first) + read_attribute(:second) end then get_value_for_column("something") would not work in case of read_attribute in get_value_for_column -Arpit Jain Anthony Ward wrote:> Frederick Cheung wrote: > >> On May 5, 4:53�pm, Anthony Ward <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt> >> wrote: >> >>> ***** �I wanted to know if it is possible to achieve what I would like >>> to do >>> � � � �as the first solution is impossible to do for various reasons >>> **** >>> >> well there''s always read_attribute (and remember that >> foo.read_attribute(:bar) is the same as foo[:bar] ) >> >> Fred >> > > Hi, > > thank you, just discovered read_attribute a few minutes ago!! > > again thank you >
On 5 May 2009, at 15:04, arpit <arpitjain11-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I think rather than doing read_attribute, you should do > > def get_value_for_column(attribute) > send(attribute) > endSort of depends what problem you are trying to use (eg send won''t work if you had a legacy schema with a column name that isn''t a legal name for a ruby method). Fred> > > because if you have > > def something > read_attribute(:first) + read_attribute(:second) > end > > then get_value_for_column("something") would not work in case of > read_attribute in get_value_for_column > > -Arpit Jain > > Anthony Ward wrote: >> Frederick Cheung wrote: >> >>> On May 5, 4:53�pm, Anthony Ward <rails-mailing-l...@andreas-s.net> >>> wrote: >>> >>>> ***** �I wanted to know if it is possible to achieve what I wo >>>> uld like >>>> to do >>>> � � � �as the first solution is impossible to do for >>>> various reasons >>>> **** >>>> >>> well there''s always read_attribute (and remember that >>> foo.read_attribute(:bar) is the same as foo[:bar] ) >>> >>> Fred >>> >> >> Hi, >> >> thank you, just discovered read_attribute a few minutes ago!! >> >> again thank you >> > > > >
yeah you are right. Thanks for clarifying :) Frederick Cheung wrote:> > On 5 May 2009, at 15:04, arpit <arpitjain11-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> I think rather than doing read_attribute, you should do >> >> def get_value_for_column(attribute) >> send(attribute) >> end >> > > Sort of depends what problem you are trying to use (eg send won''t work > if you had a legacy schema with a column name that isn''t a legal name > for a ruby method). >s/isn\''t/is> Fred > >> because if you have >> >> def something >> read_attribute(:first) + read_attribute(:second) >> end >> >> then get_value_for_column("something") would not work in case of >> read_attribute in get_value_for_column >> >> -Arpit Jain >> >> Anthony Ward wrote: >> >>> Frederick Cheung wrote: >>> >>> >>>> On May 5, 4:53�pm, Anthony Ward <rails-mailing-l...@andreas-s.net> >>>> wrote: >>>> >>>> >>>>> ***** �I wanted to know if it is possible to achieve what I wo >>>>> uld like >>>>> to do >>>>> � � � �as the first solution is impossible to do for >>>>> various reasons >>>>> **** >>>>> >>>>> >>>> well there''s always read_attribute (and remember that >>>> foo.read_attribute(:bar) is the same as foo[:bar] ) >>>> >>>> Fred >>>> >>>> >>> Hi, >>> >>> thank you, just discovered read_attribute a few minutes ago!! >>> >>> again thank you >>> >>> >> > > > > >