Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio
Last active February 3, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PerezIgnacio/a767c7a024c44464698fa57c623f2a76 to your computer and use it in GitHub Desktop.
Save PerezIgnacio/a767c7a024c44464698fa57c623f2a76 to your computer and use it in GitHub Desktop.
Example of user model that uses scopes
class User < ApplicationRecord
RECENT_ORDER_DAYS = 3
RECENT_VISITED_DAYS = 7
scope :subscribed, -> { where(receive_email_notifications: true) }
scope :without_recent_views, -> { where('last_visited < ?', RECENT_VISITED_DAYS.days.ago) }
scope :for_notifications_of_similar_purchases, lambda do
joins(:orders)
.where(fulfilled: true)
.and('orders.updated_at > ?', RECENT_ORDER_DAYS.days.ago)
.subscribed
end
has_many :orders
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment