How to run dynamic code in Rails models in a production environment
I'd like to add a get_options
method to my 开发者_JAVA技巧model. However, it is my understanding that in a production environment, models only get run once. I need to use I18n in my model, and so the output will change based on the language the user chose.
How do I make something like this work in production?
class ListHourlyPay < ActiveRecord::Base
def self.get_options
ListHourlyPay.all.map(&:amount).index_by { |obj| I18n.t("activerecord.attributes.part_time.hourly_pay_options.#{obj}") }
end
end
Thanks!
p.s. I'm not even sure this doesn't work - it is just my suspicion based on what I've heard.
Yes, models are run once, but methods are called multiple times. Any time get_options
is called, the string will be re-translated - I wouldn't worry.
精彩评论