Forum: Ruby on Rails string as attribute

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
57014aac041f53d610b8609247d07385?d=identicon&s=25 Anthony Ward (ant284)
on 2009-05-05 17:53
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-05 18:19
(Received via mailing list)
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 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
57014aac041f53d610b8609247d07385?d=identicon&s=25 Anthony Ward (ant284)
on 2009-05-05 18:20
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 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
4012b0658c1e6e940414141efcf06ce6?d=identicon&s=25 arpit (Guest)
on 2009-05-06 00:04
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-06 00:24
(Received via mailing list)
On 5 May 2009, at 15:04, arpit <arpitjain11@gmail.com> 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).

Fred
4012b0658c1e6e940414141efcf06ce6?d=identicon&s=25 arpit (Guest)
on 2009-05-06 00:30
(Received via mailing list)
yeah you are right. Thanks for clarifying :)

Frederick Cheung wrote:
>
> 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
This topic is locked and can not be replied to.