Forum: Ruby on Rails activerecord model constantize question

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.
86a01abd9d77ebfc577b2890623c6170?d=identicon&s=25 Izit Izit (izit)
on 2007-07-28 17:26
(Received via mailing list)
I have a variable called column with the value of "bla" (column =
"bla")

I have a model call Location where method bla is defined (amongst many
other methods). What I would like to to is to call the method in
Location defined in the variable column.

I tried:

Location.column.constantize

but that gave me an error NoMethod.

I am sure there is a very simple way in Ruby as always :-)

who can help me?

thanks
881b7dd91c0d9287aea5bc505c10a15a?d=identicon&s=25 Ryan Carmelo Briones (Guest)
on 2007-07-28 17:39
(Received via mailing list)
bitterbal wrote:
>
>
> I have a variable called column with the value of "bla" (column =
> "bla")
>
> I have a model call Location where method bla is defined (amongst many
> other methods). What I would like to to is to call the method in
> Location defined in the variable column.
>
>

you can use Ruby's send[1] method on the location object with the column
name. Like so:

column = "bla"
my_location = Location.find(1)
my_location.send(column) # calls mylocation.bla

[1] http://www.ruby-doc.org/core/classes/Object.html#M000334

---
Ryan Carmelo Briones


--
View this message in context:
http://www.nabble.com/activerecord-model-constanti...
Sent from the RubyOnRails Users mailing list archive at Nabble.com.
86a01abd9d77ebfc577b2890623c6170?d=identicon&s=25 Izit Izit (izit)
on 2007-07-28 17:42
(Received via mailing list)
Thanks ! I was already reading through the API docs did not find it
yet

On Jul 28, 5:38 pm, Ryan Carmelo Briones
This topic is locked and can not be replied to.