Settings table from schema:
create_table "settings", :force => true do |t|
t.string "name"
t.string "value"
t.datetime "created_at"
t.datetime "updated_at"
end
Settings table has these records:
{name: "notification_email", value: "hello@monkey.com"}
{name: "support_phone", value: "1234567"}
I want the Setting.notification_email function to return “hello@monkey.com” and respectively the Setting.support_phone function to return “1234566“.
Here is what I have in my setting.rb:
class Setting < ActiveRecord::Base
class << self
all.each do |setting|
define_method "#{setting.name}".to_sym do
setting.value.to_s
end
end
end
end
But then when I enter Setting.notification_email in console, it gives me an error:
NameError: undefined local variable or method `all' for #<Class:0x000000053b3df0>
from /home/adam/Volumes/derby/app/models/setting.rb:7:in `singletonclass'
from /home/adam/Volumes/derby/app/models/setting.rb:2:in `<class:Setting>'
from /home/adam/Volumes/derby/app/models/setting.rb:1:in `<top (required)>'
...
Use define_singleton_method – i.e.