views:

24

answers:

1

I have added some constants to a model say MyModel.rb as shown below.

MY_CONST = {
  :foo =>"Some Name for Foo",
  :bar =>"Some Name for Bar"
  }

Also I have saved string foo as the column value in a table record.

@m = MyModel.find(1)
@m.column_name #=> foo

Now in my view I need to show "Some Name for Foo" as the output for @m.column_name instead of foo

I tried MyModel::MY_CONST[:foo] and it prints "Some Name for Foo" as the output. But I don't know how to pass @m.column_name to MyModel::MY_CONST[....] dynamically.

A: 

Try

MyModel::MY_CONST[@m.column_name]

or

MyModel::MY_CONST[@m.column_name.to_sym]
Salil
I tried `MyModel::MY_CONST[@m.column_name]` it never worked for me, (that's why I posted the question) But `MyModel::MY_CONST[@m.column_name.to_sym]` works, thanks a lot.
randika
if you want to run `MyModel::MY_CONST[@m.column_name]` change `:foo =>"Some Name for Foo",` to `"foo" =>"Some Name for Foo",`
Salil
Thanks a lot Salil. I understand now, I really never thought about it.
randika