Rails model constants with hash
I have added some constants to a model say MyModel.rb
as shown below.
MY_CONST = {
:foo =>"Some Name for Foo",
:bar =>"Some Na开发者_StackOverflow社区me 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.
Try
MyModel::MY_CONST[@m.column_name]
or
MyModel::MY_CONST[@m.column_name.to_sym]
精彩评论