Skip to content

Instantly share code, notes, and snippets.

@rkellermeyer
Last active December 20, 2015 16:59
Show Gist options
  • Save rkellermeyer/6165206 to your computer and use it in GitHub Desktop.
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)
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