Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio
Created January 31, 2020 14:01
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/cee4e706abf50ed707b05adca61acbca to your computer and use it in GitHub Desktop.
Save PerezIgnacio/cee4e706abf50ed707b05adca61acbca to your computer and use it in GitHub Desktop.
Example of Query Object for User Model
class UsersQuery
attr_reader :relation
def initialize(relation = User.all)
@relation = relation
end
def subscribed
relation.where(receive_email_notifications: true)
end
def without_recent_views
relation.where('last_visited < ?', User::RECENT_VISITED_DAYS.days.ago)
end
def for_notifications_of_similar_purchases
relation.joins(:orders)
.where(fulfilled: true)
.and('orders.updated_at > ?', User::RECENT_ORDER_DAYS.days.ago)
.where(receive_email_notifications: true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment