[英]activerecord setting in scope
我有一個表business_settings
,該表使用key
和value
列存儲業務設置。
我寫了一個幫助程序來收集這些值:
def bus_setting(bus_key)
bus_setting = BusinessSetting.where(key: bus_key).first
return bus_setting.nil? ? bus_key : bus_setting.value
end
在這種情況下,返回的值是值為90的整數。
這是我要寫的作用域,但是助手bus_setting
導致“ Class:0x00的未定義方法'bus_setting'...”
scope :due, -> { where("mass_verification_date < ?", bus_setting('MASS_VERIFICATION_INTERVAL')to_i.days.ago) }
我是要寫這種方式還是犯了一個愚蠢的錯誤? 謝謝
編輯:此范圍實現了我追求的結果,但是我不想對值進行硬編碼。 scope :due, -> { where("mass_verification_date < ?", 90.days.ago) }
static scope:
scope :due, -> { where("mass_verification_date < ?", 90.days.ago) }
就您而言,這90.days.ago是靜態的。 如果要使其動態,則應在此范圍內使用參數。
在下面的示例中,scope:due,->(n),這里n是在評估where條件時將使用的參數。
Make it dynamic:
scope :due, ->(n) { where("mass_verification_date < ?", n.days.ago) }
現在,使用參數值調用此特定范圍:3將獲取所有具有mass_verfication_date <3.days.ago的業務設置。
Call this :
BusinessSetting.due(3)
@Ajay,感謝您的輸入-我實際上已經實現了混合解決方案:
mass_verification.rb
scope :due, ->(n) { where("mass_verification_date < ?", n.to_i.days.ago) }
def mass_interval
mass_interval = bus_setting("MASS_VERIFICATION_INTERVAL")
end
並啟動bus_setting
助手:
include BusinessSettingsHelper
調用如下所示: MassVerification.due(mass_interval)
再次感謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.