我有几个具有office_id和fiscal_year_id属性的模型。我想在查询前自动设置这些字段。因此,我不必关心从一个办公室到另一个办公室以及从一个财年到另一个财年的数据重叠。
发布于 2020-02-15 22:18:10
ActiveSuppport::CurrentAttributes提供了您需要的内容。
请参见文档here。
发布于 2020-02-15 18:50:57
app/models/concern/facalable.rb
module Fiscalable
extend ActiveSupport::Concern
def office_id
super || 1 # default office_id
end
def fiscal_year_id
super || Time.zone.now.year
end
end在你们的模型中。
class Model < ActiveRecord
include Fiscalable
...
end在视图中。
<%= @object.fiscal_year_id %>https://stackoverflow.com/questions/60237894
复制相似问题