Yuval Karmi
Yuval Karmi

Reputation: 26713

How to run dynamic code in Rails models in a production environment

I'd like to add a get_options method to my 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.

Upvotes: 0

Views: 307

Answers (1)

Skilldrick
Skilldrick

Reputation: 70859

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.

Upvotes: 3

Related Questions