Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio

PerezIgnacio/user_with_scopes.rb Secret

Last active Feb 3, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.