Reputation: 241
I have a table business_settings
that uses key
and value
columns to store the settings for a business.
I have written a helper to gather these values:
def bus_setting(bus_key)
bus_setting = BusinessSetting.where(key: bus_key).first
return bus_setting.nil? ? bus_key : bus_setting.value
end
in this scenario the value returned is an integer with the value 90.
This is the scope i'm attempting to write, however the helper bus_setting
causes "undefined method `bus_setting' for Class:0x00..."
scope :due, -> { where("mass_verification_date < ?", bus_setting('MASS_VERIFICATION_INTERVAL')to_i.days.ago) }
Am I going about this the write way or have I made a stupid mistake? Thanks
EDIT: This scope achieves the outcome I'm after, but I don't want to hard code the value.
scope :due, -> { where("mass_verification_date < ?", 90.days.ago) }
Upvotes: 0
Views: 53
Reputation: 241
@Ajay, thanks for your input - I've actually implemented a hybrid solution:
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
and to get the bus_setting
helper to fire:
include BusinessSettingsHelper
With the call looking like this:
MassVerification.due(mass_interval)
Thankyou again.
Upvotes: 0
Reputation: 4251
static scope:
scope :due, -> { where("mass_verification_date < ?", 90.days.ago) }
In your case, this 90.days.ago is kind of static. If you want to make it dynamic then you are supposed to use arguments for this scope.
In below example, scope :due, ->(n), here n is the argument which will be used while evaluation of the where condition.
Make it dynamic:
scope :due, ->(n) { where("mass_verification_date < ?", n.days.ago) }
Now, on calling this particular scope with argument value: 3 will fetch all the business settings having mass_verfication_date < 3.days.ago
Call this :
BusinessSetting.due(3)
Upvotes: 1