-
-
Save PerezIgnacio/a767c7a024c44464698fa57c623f2a76 to your computer and use it in GitHub Desktop.
Example of user model that uses scopes
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 < 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