Last active
December 20, 2015 16:59
-
-
Save rkellermeyer/6165206 to your computer and use it in GitHub Desktop.
Can you build a scope by model method value? (Edited showing non-working scope vs. working scope)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
# ... | |
# Old scope: scope :adult_purchases_enabled, -> { where(self.age >= 21) } | |
# Working scope: | |
scope :adult_purchases_enabled, -> { where('birth_date <= ?', Time.current.advance(years: -21)) } | |
# ... | |
# Public Methods | |
def age | |
(Time.now.utc().to_s(:number).to_i - self.birth_date.to_time.utc().to_s(:number).to_i)/10e9.to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment