scope

scope :valid_user, -> (user_key, secret_token) { if [user_key, secret_token].all? then where("user_key = ? AND secret_token = ? AND secret_token_expiry >= now()", user_key, secret_token) end }

scope always returns a chainable object So if you intend to do something like this:

# model
scope :matched_thing, -> { where(...).take }

# invoke
thing = model.matched_thing
# even there is nothing matched, it still returns `ActiveRecord::Relation []`
if thing
  this will always be executed
else
  #....
end
# take it outside of scope
thing = model.matched_thing.take
# convert [] to nil, ["active_record"] to active record

or

if thing.present?
   t = thing.take
   # unwrap active record from relation
   ...
else
   ...
end

Reference

results matching ""

    No results matching ""